用nio替换commons.io FileUtils.listFiles

时间:2014-05-21 13:25:38

标签: java nio directory

我需要替换commons.io FileUtils.listFiles因为处理bigs目录非常慢。 我读到java nio更快。 我的问题是,如何用nio替换这个方法:

public List<File> listFiles(){
    File dir = new File(this._dir);
    List<File> files = (List<File>) FileUtils.listFiles(dir, this.get_extensions(), true);

    return files;
}

谢谢!

执行以下测试:

public List<File> listFiles(){
    File dir = new File(this._dir);
    Path dire = FileSystems.getDefault().getPath( this._dir );
    long start = System.currentTimeMillis();
    try {
        Files.walkFileTree(dire, new FileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                // here you have the files to process
                if (file.toString().endsWith("IDC.xml")) {
                    System.out.println(file);

                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
               return FileVisitResult.TERMINATE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
              return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    long stop = System.currentTimeMillis();
    System.out.println( "NIO: " + (stop - start) + " ms" );
     start = System.currentTimeMillis();
    List<File> files = (List<File>) FileUtils.listFiles(dir, this.get_extensions(), true);
     stop = System.currentTimeMillis();
    System.out.println( "IO: " + (stop - start) + " ms" );
    return files;
}

nio的时间比io短。


我找到了下一个解决方案:

public List<String> listFiles(){
    File dir = new File(this._dir);
    Path dire = FileSystems.getDefault().getPath( this._dir );
    long start = System.currentTimeMillis();
    try {
        Files.walkFileTree(dire, new FindTextFilesVisitor());
    } catch (IOException e) {
        e.printStackTrace();
    }
    long stop = System.currentTimeMillis();
    System.out.println( "NIO: " + (stop - start) + " ms" );

    return this._lista;
}
public Boolean changeString( String str ) {
    this._lista.add(str);
    return true;
}
/**
 * FindTextFilesVisitor.
 */
public class FindTextFilesVisitor extends SimpleFileVisitor<Path> {
    @Override
    public FileVisitResult visitFile(Path file,
                                     BasicFileAttributes attrs)
            throws IOException {
        if (file.toString().endsWith("IDC.xml")) {
            System.out.println(file.getParent());
            changeString(file.getParent().toString());
        }
        return FileVisitResult.CONTINUE;
    }
}

1 个答案:

答案 0 :(得分:1)

使用NIO2,您可以创建一个流,以便不是所有内容都保存在内存中。 所以,你可能希望做这样的事情:

DirectoryStream.Filter<Path> pathFilter = ...
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(directory), pathFilter)) {
    for (Path path : directoryStream) {
        ...
    }
} catch (IOException ex) {}

请注意,重要的是不要将流转换为列表,否则会有什么意义,对吧?所以,您也应该更改方法签名。