Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the fastest way to scan a very large file in java?

Imagine I have a very large text file. Performance really matters.

All I want to do is to scan it to look for a certain string. Maybe I want to count how many I have of those, but it really is not the point.

The point is: what's the fastest way ?

I don't care about maintainance it needs to be fast.

Fast is key.

like image 323
chacko Avatar asked Feb 03 '11 12:02

chacko


1 Answers

For a one off search use a Scanner, as suggested here

A simple technique that could well be considerably faster than indexOf() is to use a Scanner, with the method findWithinHorizon(). If you use a constructor that takes a File object, Scanner will internally make a FileChannel to read the file. And for pattern matching it will end up using a Boyer-Moore algorithm for efficient string searching.

like image 66
Joel Avatar answered Sep 28 '22 03:09

Joel