Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.
Problem with RandomAccessFile
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 · ·