With the introduction of functional-style syntax and several new Stream based methods in Java 8, file handling finally becomes fun and easy. In fact, the new helper methods and lambda syntax even gives Python a run for its money when it comes to compact code.

Here is how you could read all lines of a file, given as a Path p (since Java 7), and output to stdout.


To make it a bit more clear what is going on, here a bit more is included and broken up.

Path p = Paths.get("myfile");
Stream lines = Files.lines(p);

A similarly neat helper function exists for recursively walking over the directory tree of the file system. Again, this prints to stdout.