Streaming files and moving them after read

后端 未结 4 529
心在旅途
心在旅途 2021-02-02 12:24

I want to stream the lines contained in files but MOVING each file to another folder once it has been processed.

The current process is like this:

Explanation:

4条回答
  •  [愿得一人]
    2021-02-02 13:07

    Actually it will be very easy if you can divide the logic into different method

     public Path readFile(File eachFile) {
       BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    
      //try-with-resources
     try (Stream lines = reader.lines()) {
    lines.forEach(System.out::println);
    
     } 
    catch (IOException e) {
       e.printStackTrace();
     }
       return eachFile.toPath();
      }
    

    And then call this method for each file

    (1)    Stream.generate(localFileProvider::getNextFile)
    (2)          .map(this::readFile) //process each file
    (3)          .forEach(path->Files.move(path,Paths.get("new path"))); //then move each file
    

提交回复
热议问题