I\'m currently trying to read lines from a text only file that I have. I found on another stackoverflow(Reading a plain text file in Java) that you can use Files.lines(..).f
Sample content of test.txt
Hello
Stack
Over
Flow
com
Code to read from this text file using lines() and forEach() methods.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FileLambda {
public static void main(String JavaLatte[]) {
Path path = Paths.get("/root/test.txt");
try (Stream lines = Files.lines(path)) {
lines.forEach(s -> System.out.println(s));
} catch (IOException ex) {
// do something or re-throw...
}
}
}