Reading text files using the Stream API in Java 8
January 18, 2015 Leave a comment
We discussed the Java 8 Stream API thoroughly on this blog starting here. We mostly looked at how the API is applied to MapReduce operations to analyse data in a stream.
The same API can be applied to File I/O. Java 8 adds a new method called “lines” to the BufferedReader object which opens a Stream of String. From then on it’s just standard Stream API usage to filter the lines in the file – and perform other operations on them in parallel such as filtering out the lines that you don’t need.
Here’s an example how you can read all lines in a file:
String filename = "c:\\logs\\log.txt"; File logFile = new File(filename); try (BufferedReader reader = new BufferedReader(new FileReader(logFile));) { StringBuilder fileContents = new StringBuilder(); Stream<String> fileContentStream = reader.lines(); fileContentStream.forEach(l -> fileContents.append(l).append(System.lineSeparator())); System.out.println(fileContents.toString()); } catch (IOException ioe) { }
We simply append each line in the stream to a StringBuilder.
In my case the log.txt file has the following contents:
hello
this is a line
next line
this is another line
…and this is yet another line
goodbye
As we’re dealing with the Stream API the usual Map, Filter and Reduce methods are all available to be performed on the text. E.g. what if you’re only interested in those lines that start with “this”? Easy:
fileContentStream.filter(l -> l.startsWith("this")) .forEach(l -> fileContents.append(l).append(System.lineSeparator()));
The StringBuilder will now only hold the following lines:
this is a line
this is another line
You can also use the Path and Files objects that were introduced in Java 7. The Files object too was extended with a method to get hold of the Stream object. The below example is equivalent to the above:
Path logFilePath = Paths.get("C:\\logs\\log.txt"); try (Stream<String> fileContentStream = Files.lines(logFilePath)) { StringBuilder fileContents = new StringBuilder(); fileContentStream.filter(l -> l.startsWith("this")) .forEach(l -> fileContents.append(l).append(System.lineSeparator())); System.out.println(fileContents.toString()); } catch (IOException ioe) { }
View all posts related to Java here.