To read a file, I would use Files
. This is quite simple and provide some methods that allow you to read each line easily :
// Get the path for the file
Path path = Paths.get( "/tmp/files.txt" );
// Read a file a print in console.
Files.lines(path) //get a `Stream<String>`
.forEach(System.out::println);
Now, in your case, you need to distinct action, for each line, you want to read another file. So let use a method that read a file and execute an action for each line :
public static void readFile(Path path, Consumer<String> action) {
try {
Files.lines(path).forEach(action);
} catch (IOException e) {
System.err.println(e);
}
}
And this will be quite simple to use. For each line of the first file, we call the method to print each line in the console with (s) -> readFile(Paths.get(s), System.out::println)
.
Path path = Paths.get( "/tmp/files.txt" );
readFile(path, (s) -> readFile(Paths.get(s), System.out::println));
That will be a recursive call using another action to print the content.
/tmp/files.txt
/tmp/file1.txt
/tmp/file2.txt
/tmp/file3.txt
/tmp/file1.txt
Hello world
/tmp/file3.txt
Foo bar
Hello world
Foo bar
java.nio.file.NoSuchFileException: /tmp/file2.txt