Search and replace with NIO and Regex?

M

Mark McKay

I've used the java.util.regex and java.io libraries quite a bit, but
am unfamiliar with java.nio.

I have several files on disk that I want to open, perform a search and
replace on, and save th emodified file back to disk. Since this only
needs a front-to-back scan of the file to accomplish, I thought it may
be possible to do this transparently with the Regex and NIO libraries
directly and avoid having to read the entire file into memory.

Can this be done simply? Could someone suggest sample code for
opening and performing a search/replace operation on a file?

Thanks.

Mark McKay
 
J

Jesper Nordenberg

I've used the java.util.regex and java.io libraries quite a bit, but
am unfamiliar with java.nio.

I have several files on disk that I want to open, perform a search and
replace on, and save th emodified file back to disk. Since this only
needs a front-to-back scan of the file to accomplish, I thought it may
be possible to do this transparently with the Regex and NIO libraries
directly and avoid having to read the entire file into memory.

The regex part is quite easy. Just implement the
java.lang.CharSequence interface and make it read characters from a
java.nio.channels.FileChannel. You can also map it into memory using
FileChannel.map(). That might improve performance.

The replace part is easy iff you only overwrite existing characters in
the file. If you need to insert or remove characters you need to
rewrite the entire file to disk which can be very slow.

/Jesper Nordenberg
 
H

hiwa

I've used the java.util.regex and java.io libraries quite a bit, but
am unfamiliar with java.nio.

I have several files on disk that I want to open, perform a search and
replace on, and save th emodified file back to disk. Since this only
needs a front-to-back scan of the file to accomplish, I thought it may
be possible to do this transparently with the Regex and NIO libraries
directly and avoid having to read the entire file into memory.

Can this be done simply? Could someone suggest sample code for
opening and performing a search/replace operation on a file?

Thanks.

Mark McKay

Code:
FileInputStream fis = new FileInputStream(args[0]);
FileChannel fc = fis.getChannel();
MappedByteBuffer mbf
= fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
byte[] barray = new byte[(int)(fc.size())];
mbf.get(barray);
String str = new String(barray, "US-ASCII");
Then, you do regex on String str.
 
T

Thomas Weidenfeller

Mark said:
I've used the java.util.regex and java.io libraries quite a bit, but
am unfamiliar with java.nio.

I have several files on disk that I want to open, perform a search and
replace on, and save th emodified file back to disk. Since this only
needs a front-to-back scan of the file to accomplish, I thought it may
be possible to do this transparently with the Regex and NIO libraries
directly and avoid having to read the entire file into memory.

A few remarks:

It can be faster using nio and memory-mapping, but if you have to search
a file to the end, you will in the end have read all the data into
memory, but with a potentially faster read method.

If you don't need the full power of regular expressions, and if you
encounter a bottleneck in the search, you might want to have a look at
text search algorithms like Knuth-Pratt-Morris or Boyer-Moore.


/Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top