File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Delete line in file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Delete line in file" Watch "Delete line in file" New topic

Delete line in file

Moniphal Say
Ranch Hand

Joined: Aug 11, 2004
Posts: 48
Dear all,

How to delete 1 line from a file with a particular match.
For example, I want to delete line in a file which is match the word "toto" ??
I just can write and read line from file but I don't know how to delete line in file??

Any idear? please help


<b>The more we learn, the more we know !</b>
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

There's no particular way to do this other than by reading in each line, checking for the pattern, and writing to a new file only those lines that don't match.

[Jess in Action][AskingGoodQuestions]
Moniphal Say
Ranch Hand

Joined: Aug 11, 2004
Posts: 48
Ok, I think so but there would be much costing if the file size is big.
If there's somewhere that Java provide more choice on this, it would be great.

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Well, there are a few other choices available, but they're going to be more complex to deal with, and for most applications, probably not worth the effort. (Especialy since you're posting this in Beginner - the alternate technique I'm going to describe is not recommended for a beginner.) I would say they only have a chance of being faster if the number of lines to delete is very small, and / or close to the end of the file. It's almost certainly best to first do this as EFH described, and see how fast it actually is. If it is indeed too slow that way, then explore this alternate technique.

There are three classes I know of which allow you to manipulate file contents directly, without writing a new file. They are:


Actually all 3 are somewhat related - you can get a FileChannel from a RandomAccessFile, and a MappedByteBuffer from a FileChannel. The MappedByteBuffer is probably fastest for a really big file - but that's not guaranteed, and it may be limited by how much memory you have. So ultimately you may have to try each one to see how fast it really is for you. The techniques for using all 3 will be similar though. If you're not familiar with any of these, start with the RandomAccessFile first.

For all 3 of these classes, there's no existing method to simply delete a range of bytes. You will have to do this by reading a range of bytes into a buffer, then copying them to a new location. Which is much like the technique EFH described, but here you have a much greater chance of screwing something up. So again, this is only really worthwhile (maybe) if there's a substantial portion of the beginning of the file which you don't need to move at all.

Let's say you have a file 10000 bytes long, and you need to delete the following regions:


First, you don't need to touch anything from 0-8000. Great. Now how to delete 8000-8100? You could take all the bytes from 8100-10000 and copy them forward to 8000-9900. However this would be inefficient, since you're going to have to move many of the later bytes again to delete subsequent lines. Instead, you'd want to do something like this:

Copy 8100-8500 to 8000-8400.
Copy 8600-9000 to 8400-8800.
Copy 9100-9500 to 8800-9200.
Copy 9600-10000 to 9200-9600.
Set file length to 9600, deleting any remaining bytes.

If you're comfortable figuring out how to code something like this, then this technique may be worth trying. Note that if you ever want to insert lines, or even edit lines in a way that might increase their length, then this won't work because you will end up overwriting some bytes before you have a chance to copy them. It may be possible to approch this instaed from the other side, starting at the end of the file. But really, that's going to be a very complex, ugly thing to do. I do not recommend it.

Again, I strongly recommend trying this the way EFH recommended first. It will definitly be easier, and it's much more flexible if you want to do other things like insert or edit lines.

Good luck...

"I'm not back." - Bill Harding, Twister
Moniphal Say
Ranch Hand

Joined: Aug 11, 2004
Posts: 48
Thanks so much for your detail help. It's clearly explained
I'll go through all these things, firstly EFH and so on. I'll comming back if I still have problem with this.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Here's a trick I picked up from IBM's VM/CMS operating system for the general "rewrite a file" problem:

read original
write temp
rename original to backup
rename temp to original
erase backup

This minimizes the odds of losing data if your copy routine fails somehow.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Delete line in file
It's not a secret anymore!