Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

delete/remove first line(s) in a text file

Thread view: 
Rob Shepherd - 03 May 2007 10:06 GMT
Dear c.l.j.programmers,

I'd like to perform (efficiently) the removal of the first n lines
from a plain text file.

Does anybody have any suggestions

Cheers

Rob
visionset - 03 May 2007 10:29 GMT
> Dear c.l.j.programmers,
>
> I'd like to perform (efficiently) the removal of the first n lines
> from a plain text file.
>
> Does anybody have any suggestions

To do exactly as you say you would need a RandomAccessFile - see API docs
there are readLine() and writeBytes(String) methods, so you would seek
forward x lines readLine and then seek back x lines to writeBytes, where x
is the number of lines you wish to skip and you would need to know the
length of these lines, because you know or by seeking 1 char at a time and
testing for line terminator.
So obviously this is a pain and proably slow, coupled with the fact that
properly buffered serial access is *very* fast, I'd just read from one file
then write it out to another.

new BufferedReader(new FileReader("mySourceFile.txt"));
new BufferedWriter(new FileWriter("myOutFile.txt"));
When you're done rename the file.

Signature

Mike W

J - 06 May 2007 02:50 GMT
>> Dear c.l.j.programmers,
>>
[quoted text clipped - 16 lines]
> new BufferedWriter(new FileWriter("myOutFile.txt"));
> When you're done rename the file.

If the file is small, why not just read the whole thing into an
ArrayList of type String then write back from element 1 to the last element?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.