aspose file tools*
The moose likes I/O and Streams and the fly likes Need help Read the File and replace its Header contents Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Need help Read the File and replace its Header contents" Watch "Need help Read the File and replace its Header contents" New topic
Author

Need help Read the File and replace its Header contents

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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 .


Save India From Corruption - Anna Hazare.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Read the file into memory (perhaps a List<String> to hold the lines), modify as needed, write back to the file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Jun 01, 2010
Posts: 618
Please post your code.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

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

Joined: Sep 07, 2004
Posts: 781

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.



Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help Read the File and replace its Header contents