*
The moose likes I/O and Streams and the fly likes editing a line of a txt file with a thousands of line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "editing a line of a txt file with a thousands of line" Watch "editing a line of a txt file with a thousands of line" New topic
Author

editing a line of a txt file with a thousands of line

Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
i think it is not advisable to store the thousands of line in the memory using Buffering, what is the best API in java, that i can store it in a temporary txt,.

or how can i edit the text file, let say i want to edit directly to line number 100?

any help would be much appreciated
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

If you can't store the contents in memory the best option is to use a temporary file. In short:
1) create a temporary file (perhaps using File.createTempFile)
2) read a line from the original file, modify as needed, write it to the temporary file
3) repeat step 2) until done
4) delete the original file
5) rename the temporary file to the original file (renameTo will also move files if the parent folder is different)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
Wouldn't using random access make sense when you need to 'modify' specific portions of the file?
-> http://java.sun.com/docs/books/tutorial/essential/io/rafs.html


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Peter Taucher wrote:Wouldn't using random access make sense when you need to 'modify' specific portions of the file?
-> http://java.sun.com/docs/books/tutorial/essential/io/rafs.html


Modify as in "change the value of individual bytes" then yes but modify as in "insert or delete some bytes" then most definitely not.


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: 19674
    
  18

And even replacing is hard if you're using an encoding that doesn't necessarily use 1 byte per character.
Roldan Baldo
Ranch Hand

Joined: Aug 11, 2009
Posts: 99
thanks, i have a problem in writing in the temporary file,
will i use


how will i insert it to the temporary file.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Not like this. First of all, you'll need the File returned by File.createTempFile("temporary","bh",file), as this is the file you're going to write to, not the original file. Haven't you read my first post? It basically includes the entire algorithm in normal words; all you need to do is translate it into code (hint: steps 2 and 3 are a loop).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: editing a line of a txt file with a thousands of line