• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
reply
    Bookmark Topic Watch Topic
  • New Topic