Read text file line by line: Java 7
package co.vorobyev.snippets.read.line.java7;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static java.nio.file.Files.newBufferedReader;
import static java.nio.file.Paths.*;
public class Read {
private void process(String line) {
System.out.println(line);
}
public void read() throws IOException {
try (BufferedReader reader = newBufferedReader(get("file.txt"), StandardCharsets.UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
process(line);
}
}
}
}