Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help Read the File and replace its Header contents

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have a File as shown



I have to read this file , and put some symbols(Tilde) in the Header after some characters the header (the First Line of the File)


Means eventully i want to have File Header like this


Please give me an idea as how to proceed with this requirement

I want an Idea as how to do with this .

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the file into memory (perhaps a List<String> to hold the lines), modify as needed, write back to the file.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for the response ,

I dont want to read the data into Memory

The Problem is when i am using RandomAccessFile and using seek(0) , and writing using writeChar('~')

The File is being corrupted , means some charaacters are being reversed or an extra space is being added.

Please help.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello , its a sample program

I am Reading the File using




HEADER1234567890

The result appearing as

HEADER ~1234567890 // An extra space is being inserted .

I also need to insert ~ after read other charatcres of the Headers also later , say after 5 in the Header .

HEADER ~12345~67890 // An extra space is being inserted .

It is removing reversing 45 into 54 and then being ~ being inserted .

can anybody please help me .



 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems you think that you can use RandomAccessFile to insert bytes. You can't. You can use it to replace bytes or to append bytes to the file but you cannot insert bytes.

One approach you can use is to create a new file by reading the original file a line at a time and writing the lines to the new file after making any modifications you require. Note, this only requires one to hold in memory one line at a time.

Once you have created the new file you can delete the old file and rename the new file. To be safe, rather than deleting the old file one normally renames it to a temporary name and then only delete the temporary file once the new file has been successfully renamed.

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sabre wrote:It seems you think that you can use RandomAccessFile to insert bytes. You can't. You can use it to replace bytes or to append bytes to the file but you cannot insert bytes.

Correct. You can "shift" all bytes after the insertion point "up" but I would much rather use your second approach.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic