-
Problem Writing to a RAF
Hi,
Sorry if this is the wrong thread, I figured I am decently new to java. I try not to ask many questions, but this is one that I just can't solve.
I'm writing a program in Netbeans for Mac that allows the storing and editing of the student's timetables in my school (I'm a student - this is coursework).
Absolutely everything works perfectly, except for deleting a student.
Basically, I replace the first 10 characters of the student that is to be deleted with "XXXXXXXXXX" for example.
I then create a new file, and copy all the students into the new file (so long as there isn't XXXXX... In their name), delete the old file, and rename the new file.
The problem I have is with the method RAF.writeUTF();.
I'm sorry if it's messy, I've not got the hang of perfect programmer styles..
Code:
//Copy entries in file 1, to file 2, excluding any beginning with x's.
try{
RandomAccessFile RAF = new RandomAccessFile("Students.csv", "rw");
RandomAccessFile RAF2 = new RandomAccessFile("Students2.csv", "rw");
//Ensure both files seek are at the beginning
//(Not sure this is necessary, but I included it anyway.)
RAF.seek(0);
RAF2.seek(0);
//While the file pointer has not reached the end of the file{
while(RAF.getFilePointer() < RAF.length()){
//Store filepointer.
Int filePointer = RAF.getFilePointer();
//if line [I]does not[/I] start with "XXXXXXXXXX"
if(!(RAF.readLine().toString()).startsWith("XXXXXXXXXX")){
//^RAF.readLine moves the file pointer, so
//send it back to beginning of line, so it can be read again.
RAF.seek(filePointer);
//Write into RAF2, whatever the line of RAF is.
RAF2.writeUTF(RAF.readLine().toString() + "\n");
}//End if statement
}//End while
//Close the Files
RAF2.close();
RAF.close();
}//End try
catch(Exception e){
System.out.println("Something wrong copying the file");
}
//Code would then go to delete the first file.
Sorry the example is long - if I make it shorter it doesn't make sense.
My files are both ".csv".
-
The first two lines of "Students.csv", looks like this:
Code:
Niaz Ahmed,Y13,En13a1s,Fr13bh,Ec13h,Phy13h,Ma13s,Cs13s
Ben Brian,Y13,En13a1s,De13b,Psy13h,Ch13s,Ma13h,Ar13h
However, when I do the above code, the new file starts with this:
Code:
6Niaz Ahmed,Y13,En13a1s,Fr13bh,Ec13h,Phy13h,Ma13s,Cs13s
4Ben Brian,Y13,En13a1s,De13b,Psy13h,Ch13s,Ma13h,Ar13h
Now, I'm not sure, but I assume this extra bit on the front is due to the .writeUTF method, defining the length of the string perhaps.
However, this messes up all other functions of my program.
.writeChars() does not work either, as when I opened this attempt in 0xED (Hex Editor) it revealed a bunch of information about each character, that was also messing up my searches.
An awesome solution would be if there was simply another way to write to the file, but I doubt that, so I assume I must be writing somewhat incorrectly.
I also wonder if my ("\n") in each file has anything to do with it.
I figured .readLine(); would read the line and ignore the line break.
If there's any help anyone can give me, I'd be very grateful.
Sorry for the long description.
-Ewan