• Post Reply Bookmark Topic Watch Topic
  • New Topic

remove one line from a file

 
lily zou
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to remove first line content from a file, I could use BufferedReader + FileReader to read in all the entries and remove the first one and the use writer to create a new file.
I am wondering if there is a way that I could only read in the first line and delete it and close reader immediately ?
Thanks a lot,
lily
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really. I mean, there are also ways to do this with a single RandomAccessFile (yuck!) or FileChannel (much better) in read/write mode - but it will be more complicated to program. Using a FileChannel may be preferable if speed is an issue. Basically you'd need to identify the byte position of the end of the first line, then copy all later bytes into a ByteBuffer and then write them to an earlier position in the file. Finally, truncate the file at the correct new length.
Again, only do this if performance is an issue here, as it will definitely be more complex than the FileReader / BufferedReader / BufferedWriter / FileWriter solution.
[ April 21, 2003: Message edited by: Jim Yingst ]
 
lily zou
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, Jim.
I did it by using FileReader/BufferedReader pattern since performance is not a big issue here and someone else is waiting for my code to do testing.
Thanks again
Lily
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!