Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Problem with RandomAccessFile Posted by adnakoul on 18 Mar 2005 at 7:06 PM
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:

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();


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

ecrire.write(infos,0,infos.length-5);


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
Report
Re: Problem with RandomAccessFile Posted by datavirtue on 3 Jul 2005 at 12:42 PM
: 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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.