Applying streams to NIO 2.0
Collections can not only provide Stream
objects, but also NIO 2.0 classes. The following recipe will provide us with some service methods on file transactions that utilize stream operations.
Getting started
Open project ch06
again and add some services that will manage dump files containing employee information.
How to do it...
NIO 2.0 APIs are known for their stream-based operations for reading and writing files. We use NIO 2.0 in many communication and data transformation modules. In this recipe, Stream API will be applied to perform some file operations using NIO 2.0:
- Let us create another service class,
EmployeeFileStreamService
, in the packageorg.packt.functional.codes.service.impl
. Add the following method that will retrieve the context of a dump file using NIO 2.0 and Stream APIs:
public void viewFileContent(String empFile) throws IOException{ Consumer<String> readStr = System.out::println; Stream<String> content = null; Path path = Paths...