I am currently in the process of studying Java. I am interested in how to read text files the most efficient way. I read that it is possible to use a Stream object (is "object" in this context correct?) to do that and gave it a shot already. I am able to print out every line from the text document using the following code:
private static void ReadFile(String filePath) {
Stream<String> readFileStream = null;
try {
readFileStream = Files.lines(Paths.get(filePath));
} catch (IOException e) {
e.printStackTrace();
}
readFileStream.forEach(System.out::println);
}
I would like to do something using each line. For example I would like to put a bunch of names into my text document and have Java print out "Hello, NAME" for each name. How do I do that? How do I access a line individually, do I have to put the list into an array first in order to iterate through it?
Is it correct to say that we created a Stream object named readFileStream? I want to make sure that my terminology is right. Moreover, why do we add after Stream? I know that <> are used for lists but I do not understand them in this context.
Thanks in advance.