Created
September 18, 2021 10:58
-
-
Save GaetanoPiazzolla/f56b82733ea62e0e0b6e3ce757ed0f47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.nio.CharBuffer; | |
| import java.nio.MappedByteBuffer; | |
| import java.nio.channels.FileChannel; | |
| import java.nio.charset.CharsetDecoder; | |
| import java.nio.charset.StandardCharsets; | |
| public List<String> searchChannel() { | |
| CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder(); | |
| final FileChannel channel = new FileInputStream(pathFile).getChannel(); | |
| MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); | |
| CharBuffer decodedBuffer = decoder.decode(buffer); | |
| String[] a = decodedBuffer.toString().split("\n"); | |
| List<String> found = new ArrayList<>(); | |
| for (String s : a) { | |
| if (s.contains(SEARCH_TOKEN)) { | |
| found.add(s); | |
| } | |
| } | |
| return found; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
related to medium article https://gae-piaz.medium.com/search-string-in-file-with-java-as-fast-as-possible-fedafdc7a3ee