I need help with RandomAccessFile. I have looked everywhere without success. Here is what I want to do: I create a file that I want to update through my program. The change must occur at a specific location. This is why I use RandomAccess.
Assume the file is like that
3.Assigned to: Paul
Now if the user wants to reassign a task to "Robert" or maybe add another person to the task , line 3 of the same file must be updated. It should look like:
3.Assigned to: Paul,Robert
I first read the file in. If I hit line 2, I record the file pointer and break out of the loop. then the I type the following code:
RandomAccessfile write = new RandomAccessFile(File toUpdate,"rw");
.......// I read in here up to line 2 and record the File Pointer. Then I do...
String newLine = oldLine+","+newAssignedName;
byte [ ] infos = newLine.getBytes();
ecrire.write(infos,0,infos.length-1); //I rewrite the entire line
After I do this the file looks like this
3.Assigned To: Paul,Robertsolve:literal
if I change the statement
or somewhere close to that, it prints less and less of the Paul,Robert part and more of the next line.
It somehow, grabs the next line and overwrites it as well. I dont know where else to turn. I have gone over and over again on the API but the methods that would seem the most logic to use do not work.
Sorry its so long.
0 · ·