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:

  1. Let us create another service class, EmployeeFileStreamService, in the package org.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.get(empFile); 
  content = Files.lines(path, Charset.defaultCharset()); 
  content.map(String::toUpperCase) 
            .forEach(readStr); 
  content.close(); 
} 
  1. Add a method that opens a dump file and counts the number of employee records:
public long countRecsInFile(String empFile) throws IOException{ 
  long numWords = 0; 
  Stream<String> content = null; 
  Path path = Paths.get(empFile); 
  content = Files.lines(path, Charset.defaultCharset()); 
  numWords = content 
      .map(line -> Arrays.stream(line.split(" "))) 
      .count(); 
   content.close(); 
   return numWords; 
} 
  1. The following method searches for a directory or file based on the specified extension and returns all of the searches to the caller:
public String searchFilesDir(String filePath, String    
   extension) throws IOException{ 
 
  Path root = Paths.get(filePath); 
  int depth = 3; 
  Stream<Path> searchStream = 
    Files.find(root, depth, (path, attr) -> 
            String.valueOf(path).endsWith(extension)); 
  String found = searchStream 
      .sorted() 
      .map(String::valueOf) 
      .collect(Collectors.joining(" / ")); 
  searchStream.close(); 
  return found; 
} 
  1. The next method will search and list all documents with a preferred extension from a specific directory:
public void viewDirFiles(String dirPath, String extension) 
throws IOException{ 
  Consumer<String> readStr = System.out::println; 
  Stream<Path> stream = 
    Files.list(Paths.get(dirPath)).sequential(); 
  stream.map(String::valueOf) 
      .filter(path -> path.endsWith(extension)) 
      .forEach(readStr); 
} 
  1. The following are two methods that highlight the use of BufferedReader and Stream. The viewDirBuffered() method retrieves all documents from a specified directory, while the other method, countRecByAge(), counts the number of records having a particular age:
public void viewDirBuffered(String dirPath) throws IOException{ 
  Consumer<String> readStr = System.out::println; 
  BufferedReader buff = 
    Files.newBufferedReader(Paths.get(dirPath)); 
  buff.lines() 
      .map(String::toLowerCase) 
      .forEach(readStr); 
} 
   
public long countRecByAge(String filePath, int age) 
throws IOException{ 
  Path path = Paths.get(filePath); 
  BufferedReader reader = Files.newBufferedReader(path); 
  long numRec = reader 
       .lines() 
       .filter(line -> line.contains(age+"")) 
       .count(); 
return numRec; 
}
  1. Always close the stream after reading. Streams are not needed when writing on a file; rather, use NIO 2.0 classes and methods for writing.
  2. Create a test dump file for testing and save it in src/test/java. A sample employee_list.txt may contain data separated by spaces:
sherwin john tragura 39 
owen salvador estabillo  35 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.160.181