Free tools Windows power users keep installed
One-click scans. No signup required.
Use FileChannel.map(FileChannel.MapMode.READ_ONLY, position, size) to map a file region, then scan its bytes for the pattern. For files too large for one mapping, scan consecutive regions and carry enough overlap between them to catch matches that cross a boundary. Keep file offsets as long values and account for the setup cost and file-stability limits of mapped access.
Map a file region and search for a byte pattern
A MappedByteBuffer exposes a mapped region of a file. Java creates it through FileChannel.map. For a read-only search, use FileChannel.MapMode.READ_ONLY. Oracle’s Java SE 26 API says mapping can be more efficient for large regions, but also cautions that it is generally worthwhile only for relatively large files because mapping costs more than reading or writing a few tens of kilobytes. This is a qualitative API note, not a performance guarantee for a particular workload. Oracle FileChannel API
The mapped buffer starts at position zero, and its limit and capacity equal the requested mapping size. Buffer indexes are int-based, so search positions within a region are integers; file offsets should remain long.
Example: scan a file that fits in one mapping
This example finds every occurrence of a byte sequence and prints each match’s zero-based file offset. It is a byte-oriented search: it does not decode text or interpret character encodings.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public class MappedSearch {
static void search(Path path, byte[] pattern) throws IOException {
if (pattern.length == 0) {
throw new IllegalArgumentException("pattern must not be empty");
}
try (FileChannel channel = FileChannel.open(path, StandardOpenOption.READ)) {
long fileSize = channel.size();
if (fileSize == 0) return;
if (fileSize > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Use the multi-region version for this file");
}
MappedByteBuffer data = channel.map(
FileChannel.MapMode.READ_ONLY, 0, fileSize);
for (int i = 0; i <= data.limit() - pattern.length; i++) {
boolean matches = true;
for (int j = 0; j < pattern.length; j++) {
if (data.get(i + j) != pattern[j]) {
matches = false;
break;
}
}
if (matches) {
System.out.println("Match at byte offset " + i);
}
}
}
}
}
The example rejects patterns of length zero because an empty pattern has no useful byte offset semantics for this scan. If the file is larger than one supported mapping, use bounded regions instead of trying to map the whole file at once.
Scan files larger than one mapping
The Java SE 26 API limits a single mapped region to Integer.MAX_VALUE bytes. A file larger than that requires multiple mappings. Ensure each requested range is within the file: the API says behavior is unspecified if a mapping extends beyond the file. FileChannel.map parameters and behavior
Rank #2
Carry overlap across region boundaries
If a pattern has m bytes, retain or rescan up to m - 1 bytes from the end of one region when searching the next. Otherwise, a pattern whose first bytes are at the end of one mapping and remaining bytes at the start of the next will be missed. One straightforward strategy is to begin the next mapping at previousRegionEnd - (pattern.length - 1), while ensuring that reported matches are emitted only once.
For example, suppose the pattern is ABCD and a region ends with AB. The next region should include those final two bytes before scanning its new data. A match beginning in that overlap is reported using its absolute file offset, not its local index in the mapping.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsKeep offsets absolute
For a match at index i in a region that starts at file position regionStart, the file offset is regionStart + i. Use a long for regionStart and the resulting offset; only the index inside a particular mapped buffer is an int. Compute the region size so it is nonnegative, no larger than Integer.MAX_VALUE, and no greater than the bytes remaining in the file.
Decide whether mapping fits the workload
- Large regions: Mapping may be useful when scanning substantial file regions, but setup has a cost. Oracle's API does not promise that mapping will outperform buffered reads in every case. Oracle performance note
- Small or sequential workloads: Ordinary buffered reads may be a better fit when the file is small or the work is a simple sequential pass; measure with the actual workload rather than assuming mapping is faster.
- Very large files: Split the scan into mappings at or below the API's maximum region size, using overlap based on the pattern length.
- Mutable files: Mapping is a poor fit if another process may modify or truncate the backing file while the scan is running.
Search bytes or decoded text?
Byte-pattern matching compares the exact byte sequence. Searching for text requires an explicit character encoding and clear semantics: for example, whether matching is case-sensitive and whether normalization matters. A multibyte encoded character can straddle two mappings. If the goal is character-level text search, overlap enough encoded bytes and use a decoder that can preserve state across regions; independently decoding each region can split a character. The FileChannel.map API defines mapping behavior, not text-decoding behavior.
Rank #4
Keep the mapped file stable during the scan
Use READ_ONLY for searching so the mapping does not permit writes through that buffer. The API describes READ_WRITE changes as eventually propagated to the file and PRIVATE changes as private copies that are not propagated. FileChannel mapping modes
Mapped access depends in part on the operating system. Oracle warns that propagation of changes to mapped data or to the file's size is unspecified; if another process truncates a mapped file, mapped regions can become inaccessible and an unspecified exception can occur. Avoid modifying or truncating the file during a scan, and coordinate with processes that can change it. FileChannel mapping behavior
Best Value
Closing the originating FileChannel does not invalidate an existing mapping. The mapping remains valid until its buffer is garbage-collected, so closing the channel is not a deterministic way to unmap it. MappedByteBuffer lifetime
Quick Recap
Common mistakes to avoid
- Trying to map a region larger than
Integer.MAX_VALUEbytes as oneMappedByteBuffer. - Scanning regions independently without overlap, which misses boundary-spanning matches.
- Adding a buffer index to a file position using an
int, risking overflow for large files. - Assuming text characters always align with mapping boundaries or that raw-byte matching is equivalent to text matching.
- Mapping beyond the current file size or allowing another process to truncate the file during the scan.
- Assuming closing the channel immediately releases the mapped region.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

