• Post Reply Bookmark Topic Watch Topic
  • New Topic

First Line in a file is to be replaced.

 
shivanandini kuncham
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to replace the first line in a file.
Assume that a file contains a number of records and the first line of that file is holding the Record Count of that file.
If in future i update this file by inserting more records, then i need to update the Record Count also.
Record Count is in the first line of that file.
I just want to replace the previous count with the latest one without disturbing the file.
Inserting of more records is anyhow done by writing bytes into the file in the append mode.

Please suggest a way to accomplish this.

Thanks in advance.

-Nandini.
 
Mohit Gupta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can use RandomAccessFile class to achieve the same.
just a sample code:

import java.io.*;

class Replace
{
public static void main(String[] args) throws Exception
{
RandomAccessFile raf = new RandomAccessFile("BCNominalAccount.txt","rw");
raf.seek(0);
raf.writeBytes("1082");
}
}

BCNominalAccount.txt:
2
1,22.0,3.444
324,44,43,4234,

after running the sample the output will be:
4
1,22.0,3.444
324,44,43,4234,
1,22.0,3.444
324,44,43,4234,

This is what u r loking for
have fun
--Mohit
 
kapil munjal
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohit,

your solution doesn't seem to work.

kapi
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!