How to replace File.listFiles(FileFilter filter) with nio in Java 7?

后端 未结 1 1731
眼角桃花
眼角桃花 2020-12-16 13:59

I have some file I/0 traversal code written in Java 6, trying to move it the New I/O in Java 7 but I cannot find any replacement for this kind of stuff.

File         


        
相关标签:
1条回答
  • 2020-12-16 14:36

    Using Files#newDirectoryStream and DirectoryStream.Filter

    Here is the code:

    DirectoryStream<Path> stream = Files.newDirectoryStream(dir, new DirectoryStream.Filter<Path>() {
    
            @Override
            public boolean accept(Path entry) throws IOException 
            {
                return Files.isDirectory(entry);
            }
        });
    
    for (Path entry: stream) {
          ...
    }
    

    BTW, I omitted the exception handling in the above code for simplicity.

    0 讨论(0)
提交回复
热议问题