AccessDeniedException 如果使用 Files.find()
AccessDeniedException if using Files.find()
我需要在目录中找到所有匹配文件并获取一些信息(例如 unix 权限)。
如果我尝试使用 Files.find() 方法,它会起作用,但有些文件或目录会导致 AccessDeniedException
例如
try (Stream<Path> stream = Files.find(Paths.get("/etc/cups/ssl"), 1, (p, bfa) -> true)) {
stream.forEach(p -> {
try {
PosixFileAttributes attr = Files.readAttributes(p, PosixFileAttributes.class);
System.out.println(attr.permissions());
} catch (IOException e) {}
});
}
return :
Exception in thread "main" java.nio.file.AccessDeniedException: /etc/cups/ssl
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.newDirectoryStream(UnixFileSystemProvider.java:428)
at java.base/java.nio.file.Files.newDirectoryStream(Files.java:471)
at java.base/java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
at java.base/java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:322)
at java.base/java.nio.file.FileTreeIterator.<init>(FileTreeIterator.java:71)
at java.base/java.nio.file.Files.find(Files.java:3937)
at unix.utils.UnixFile.main(UnixFile.java:2371)
Process finished with exit code 1
但是对相同的文件使用 Process 和 command 没有任何异常
String[] command = { "/bin/sh", "-c", "/bin/ls -ldA /etc/cups/ssl" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
return:
drwx------ 2 root lp 4096 Feb 16 16:48 /etc/cups/ssl
Process finished with exit code 0
使用Files.find()时异常的原因是什么?我不改文件,不移动,只是获取一些属性信息
我知道问题出在哪里了。
我在使用过程中只得到了目录属性,并没有进入。
但是在文件的例子中,我不仅得到了目录,还试图进入里面,因为maxdepth设置为1。需要将maxdepth设置为0才能只检查这个目录而不进入。
我需要在目录中找到所有匹配文件并获取一些信息(例如 unix 权限)。
如果我尝试使用 Files.find() 方法,它会起作用,但有些文件或目录会导致 AccessDeniedException
例如
try (Stream<Path> stream = Files.find(Paths.get("/etc/cups/ssl"), 1, (p, bfa) -> true)) {
stream.forEach(p -> {
try {
PosixFileAttributes attr = Files.readAttributes(p, PosixFileAttributes.class);
System.out.println(attr.permissions());
} catch (IOException e) {}
});
}
return :
Exception in thread "main" java.nio.file.AccessDeniedException: /etc/cups/ssl
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.newDirectoryStream(UnixFileSystemProvider.java:428)
at java.base/java.nio.file.Files.newDirectoryStream(Files.java:471)
at java.base/java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
at java.base/java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:322)
at java.base/java.nio.file.FileTreeIterator.<init>(FileTreeIterator.java:71)
at java.base/java.nio.file.Files.find(Files.java:3937)
at unix.utils.UnixFile.main(UnixFile.java:2371)
Process finished with exit code 1
但是对相同的文件使用 Process 和 command 没有任何异常
String[] command = { "/bin/sh", "-c", "/bin/ls -ldA /etc/cups/ssl" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
return:
drwx------ 2 root lp 4096 Feb 16 16:48 /etc/cups/ssl
Process finished with exit code 0
使用Files.find()时异常的原因是什么?我不改文件,不移动,只是获取一些属性信息
我知道问题出在哪里了。 我在使用过程中只得到了目录属性,并没有进入。 但是在文件的例子中,我不仅得到了目录,还试图进入里面,因为maxdepth设置为1。需要将maxdepth设置为0才能只检查这个目录而不进入。