Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Roldan Baldo
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Peter Taucher
Ranch Hand
Posts: 174
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And even replacing is hard if you're using an encoding that doesn't necessarily use 1 byte per character.
 
Roldan Baldo
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic