Streams from Files
Description
java.io and java.nio.file packages from Java 8 has added many methods to support I/O operations using streams.
We can read text from a file as a stream of strings. Each element in the stream represents one line of text.
We can also use a stream to read JarEntry from a JarFile and we can read entries in a directory as a stream of Path.
Auto close
Calling the close() method on the stream will close the underlying file.
Alternatively, we can create the stream in a try-with-resources statement so the underlying file is closed automatically.
Example
The following code shows how to read contents of a file using a stream.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
/* ww w . j av a2s .com*/
public class Main {
public static void main(String[] args) {
Path path = Paths.get("./Main.java");
try (Stream<String> lines = Files.lines(path)) {
lines.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The code above generates the following result.
Example 2
The following code shows how to read a path using stream.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
// w w w . j a va2 s. co m
public class Main {
public static void main(String[] args) {
Path dir = Paths.get(".");
System.out.printf("%nThe file tree for %s%n",
dir.toAbsolutePath());
try (Stream<Path> fileTree = Files.walk(dir)) {
fileTree.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The code above generates the following result.