gpt4 book ai didi

java - 使用 java.nio.file* 进行目录和文件扫描;

转载 作者:行者123 更新时间:2023-11-30 05:50:00 27 4
gpt4 key购买 nike

如何在java.io.File中实现相同的功能;

List<File> List = (List<File>) FileUtils.listFilesAndDirs(directory, 
TrueFileFilter.TRUE, TrueFileFilter.TRUE);
String fileID = f.getAbsolutePath().replaceAll(staticRoot, "");

同时重构为 java.nio.file.*;

DirectoryStream<Path> List = Files.newDirectoryStream(Paths.get(directory));    
String fileID = f.???

什么方法会返回与 java.io.File 包中相同的输出?谢谢。

我已经尝试过

String fileID = f.toAbsolutePath().toString().replaceAll(staticRoot, "");

for (Path f : fList) {
String fileID = f.toAbsolutePath().toString().replaceAll(staticRoot, "");
System.out.println("file ID : " + fileID);
String parentID = null;
}

staticRoot =/home/test/dir1/dir10/test9.txt
结果应该剪切'/home/test/'并仅返回dir1/dir10/test9.txt

最佳答案

由于 FileUtils.listFilesAndDirs(directory, TrueFileFilter.TRUE, TrueFileFilter.TRUE) 将递归访问 directory 中的所有子目录,因此您很可能希望将其替换为 Files.walk(Path, FileVisitOption...) 方法.

Return a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file. The file tree is traversed depth-first, the elements in the stream are Path objects that are obtained as if by resolving the relative path against start.

关于java - 使用 java.nio.file* 进行目录和文件扫描;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54091105/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com