Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

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

adnakouladnakoul Posts: 68Member
Hi all,
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

1.Task: one
2.Date:
3.Assigned to: Paul
4.Resolve:literal
5.Yat:never

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:


1.Task: one
2.Date:
3.Assigned to: Paul,Robert
4.Resolve:literal
5.Yat:never

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:

[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.seek(pointer);
ecrire.write(infos,0,infos.length-1); //I rewrite the entire line
ecrire.close();
[/code]

After I do this the file looks like this

1.Task: one
2.Date:
3.Assigned To: Paul,Robertsolve:literal
5.Yat:never

if I change the statement

[code]ecrire.write(infos,0,infos.length-5);[/code]

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.

Please help
Sorry its so long.
Thanks java.lang

Comments

  • datavirtuedatavirtue Posts: 12Member
    : Hi all,
    : 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.


    You need this book:

    Murachs beginning Java 2

Sign In or Register to comment.