BAEL-6545: Update article https://www.baeldung.com/java-read-lines-large-file (#14168)
This commit is contained in:
parent
9918d614e6
commit
5bfb63b2a1
|
@ -1,9 +1,16 @@
|
||||||
package com.baeldung.readlargefile;
|
package com.baeldung.readlargefile;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.SeekableByteChannel;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.io.LineIterator;
|
import org.apache.commons.io.LineIterator;
|
||||||
|
@ -93,6 +100,61 @@ public class ReadLargeFileUnitTest {
|
||||||
logMemory();
|
logMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingBufferedReader_whenIteratingAFile_thenCorrect() throws IOException {
|
||||||
|
String fileName = "src/test/resources/myFile";
|
||||||
|
|
||||||
|
logMemory();
|
||||||
|
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
|
||||||
|
while (br.readLine() != null) {
|
||||||
|
// do something with each line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logMemory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingNewBufferedReader_whenIteratingAFile_thenCorrect() throws IOException {
|
||||||
|
String fileName = "src/test/resources/myFile";
|
||||||
|
|
||||||
|
logMemory();
|
||||||
|
try (BufferedReader br = java.nio.file.Files.newBufferedReader(Paths.get(fileName))) {
|
||||||
|
while (br.readLine() != null) {
|
||||||
|
// do something with each line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logMemory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingSeekableByteChannel_whenIteratingAFile_thenCorrect() throws IOException {
|
||||||
|
String fileName = "src/test/resources/myFile";
|
||||||
|
|
||||||
|
logMemory();
|
||||||
|
try (SeekableByteChannel ch = java.nio.file.Files.newByteChannel(Paths.get(fileName), StandardOpenOption.READ)) {
|
||||||
|
ByteBuffer bf = ByteBuffer.allocate(1000);
|
||||||
|
while (ch.read(bf) > 0) {
|
||||||
|
bf.flip();
|
||||||
|
// System.out.println(new String(bf.array()));
|
||||||
|
bf.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
logMemory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenUsingStreamApi_whenIteratingAFile_thenCorrect() throws IOException {
|
||||||
|
String fileName = "src/test/resources/myFile";
|
||||||
|
|
||||||
|
logMemory();
|
||||||
|
try (Stream<String> lines = java.nio.file.Files.lines(Paths.get(fileName))) {
|
||||||
|
lines.forEach(line -> {
|
||||||
|
// do something with each line
|
||||||
|
});
|
||||||
|
}
|
||||||
|
logMemory();
|
||||||
|
}
|
||||||
|
|
||||||
// utils
|
// utils
|
||||||
|
|
||||||
private final void logMemory() {
|
private final void logMemory() {
|
||||||
|
|
Loading…
Reference in New Issue