DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Throughput vs Goodput: The Performance Metric You Are Probably Ignoring in LLM Testing
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team
  • How AI Is Rewriting Full-Stack Java Systems: Practical Patterns with Spring Boot, Kafka and WebSockets

Search in Big Files Via MappedByteBuffer

Learn more about search in big files via MappedByteBuffer.

By 
Anghel Leonard user avatar
Anghel Leonard
DZone Core CORE ·
Dec. 04, 19 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
14.8K Views

Join the DZone community and get the full member experience.

Join For Free

Coffee and saucer

Learn more about search in big files via MappedByteBuffer.

This application is useful for searching in big files that don't fit well for BufferedReader,  Files.readAllLines(), Files.lines(), and Scanner.

The solution that we'll talk about here is based on Java NIO.2, MappedByteBuffer, and FileChannel. This solution opens a memory-mapped byte buffer (MappedByteBuffer) from a FileChannel on the given file. We traverse the fetched byte buffer and look for matches with the searched string (this string is converted into a byte[] and searching take place byte by byte).

For relatively small files, it is faster to load the entire file into memory (if RAM allows it). For large/huge files, it is faster to load and process the files in chunks (for example, a chunk of 5 MB). Once we have loaded a chunk, we have to count the number of occurrences of the searched string. We store the result and pass it to the next chunk of data. We repeat this until the entire file has been traversed.

Let's take a look at the core lines of this implementation (take a look at the source code bundled with this book for the complete code):

private static final long MAP_SIZE = 5242880; // 5 MB in bytes

public static long countOccurrences(Path path, String text)
                                           throws IOException {

   final byte[] texttofind = text.getBytes(StandardCharsets.UTF_8);
   long count = 0;

   try (FileChannel fileChannel = FileChannel.open(path,
                                           StandardOpenOption.READ)) {
      long position = 0;
      long length = fileChannel.size();

      while (position < length) {
         long remaining = length - position;
         long bytestomap = (long) Math.min(MAP_SIZE, remaining);

         MappedByteBuffer mbBuffer = fileChannel.map(
                          MapMode.READ_ONLY, position, bytestomap);
         long limit = mbBuffer.limit();
         long lastSpace = -1;
         long firstChar = -1;
         while (mbBuffer.hasRemaining()) {
            // code omitted for brevity available on GitHub
            ...
         }
      }
   }

   return count;
}


This solution is extremely fast because the file is read directly from the operating system's memory without having to be loaded into the JVM. The operations take place at the native level, called the operating system level. Note that this implementation works only for the UTF-8 charset, but it can be adapted for other charsets as well.

The complete application is available on GitHub.

If you enjoyed this article, then I'm sure you will love Chapter 6 from Java Coding Problem. This chapter is dedicated to file/folders manipulation in Java.

Further Reading

Comparing Files in Java

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook