我正在编写一个 Web 服务器,并试图确保我尽可能高效,最大限度地减少文件系统调用。问题在于返回流的方法,例如 java.nio.file.Files.list返回 Paths
的 Stream,我想要一个 BasicFileAttributes 的 Stream ,这样我就可以返回每个路径的创建时间和更新时间(比如返回 LDP Container 的结果)。
当然,一个简单的解决方案是 map
Stream 的每个元素,使用一个函数获取路径并返回文件属性 (p: Path) => Files.getAttributeView。 ..
但这听起来像是会为每个路径调用 FS,这似乎是一种浪费,因为要获取文件信息,JDK 不能远离属性信息。
我实际上是从 2009 OpenJDK mailing list 看到这封邮件的表示他们已经讨论过添加一个 API,该 API 将返回一对路径和属性...
我在 JDK java.nio.file.FileTreeWalker
上找到了一个非公共(public)类,它有一个允许获取属性 FileTreeWalker.Event
的 API。这实际上利用了 sun.nio.fs.BasicFileAttributesHolder
,它允许 Path 保留属性的缓存。但它不公开,也不清楚它在哪里工作。
当然还有全FileVisitor API,它具有返回 Path
和 BasicFileAttributes
的方法,如下所示:
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {...}
所以我正在寻找是否有一种方法可以将其转换为符合 Reactive Manifesto 背压原理的 Stream由 Akka 插入, 而不会占用太多资源。我查了开源Alpakka File项目,但这也在流式传输返回 Path
s ...
Files
方法
最佳答案
您可以使用 Files.find
访问带有路径的文件属性它接受 BiPredicate
BiPredicate 中的副作用操作将启用对两个对象的操作,而无需触及路径中每个项目的文件系统。使用您的谓词条件 yourPred
, 副作用 predicate
下面将收集属性供您在流处理中检索:
public static void main(String[] args) throws IOException {
Path dir = Path.of(args[0]);
// Use `ConcurrentHashMap` if using `stream.parallel()`
HashMap <Path,BasicFileAttributes> attrs = new HashMap<>();
BiPredicate<Path, BasicFileAttributes> yourPred = (p,a) -> true;
BiPredicate<Path, BasicFileAttributes> predicate = (p,a) -> {
return yourPred.test(p, a)
// && p.getNameCount() == dir.getNameCount()+1 // Simulates Files.list
&& attrs.put(p, a) == null;
};
try(var stream = Files.find(dir, Integer.MAX_VALUE, predicate)) {
stream.forEach(p-> System.out.println(p.toString()+" => "+attrs.get(p)));
// Or: if your put all your handling code in the predicate use stream.count();
}
}
模拟File.list
的效果使用一级 find
扫描仪:
BiPredicate<Path, BasicFileAttributes> yourPred = (p,a) -> p.getNameCount() == dir.getNameCount()+1;
对于大型文件夹扫描,您应该通过插入 attrs.remove(p);
清理 attrs 映射。在使用路径之后。
编辑
上面的答案可以重构为 Map.Entry<Path, BasicFileAttributes>
的 3 行调用返回流,或者很容易添加一个类/记录来保存 Path/BasicFileAttribute 对并返回 Stream<PathInfo>
相反:
/**
* Call Files.find() returning a stream with both Path+BasicFileAttributes
* as type Map.Entry<Path, BasicFileAttributes>
* <p>Could declare a specific record to replace Map.Entry as:
* record PathInfo(Path path, BasicFileAttributes attr) { };
*/
public static Stream<Map.Entry<Path, BasicFileAttributes>>
find(Path dir, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher, FileVisitOption... options) throws IOException {
HashMap <Path,BasicFileAttributes> attrs = new HashMap<>();
BiPredicate<Path, BasicFileAttributes> predicate = (p,a) -> (matcher == null || matcher.test(p, a)) && attrs.put(p, a) == null;
return Files.find(dir, maxDepth, predicate, options).map(p -> Map.entry(p, attrs.remove(p)));
}
关于java - 如何从文件系统中获取文件属性流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66699379/