[英]Files.exists(path) and path.toFile().exists() give different results for the same file
I'm getting a different result for Files.exists(path)
to path.toFile().exists()
for a local file on Windows. 对于Windows上的本地文件,我在
path.toFile().exists()
获得了不同的Files.exists(path)
结果。 I can see this file in Windows Explorer although I have (randomly) modified permissions and perhaps the permissions do not make sense. 我可以在Windows资源管理器中看到这个文件,虽然我有(随机)修改过的权限,也许权限没有意义。
However this doesn't explain why the old method returns true and the new methods returns false. 但是,这并不能解释为什么旧方法返回true并且新方法返回false。 The file definently exists but maybe it is invisible to the user running the Java code, so I'm not sure what the correct answer should be.
该文件确定存在但可能对运行Java代码的用户不可见,因此我不确定应该是什么样的正确答案。 Nor can I see how to see which user is running the code, there is only one real user Paul on the computer, but I'm wondering if whether if run as administrator or not effects things.
我也无法看到如何查看哪个用户正在运行代码,计算机上只有一个真正的用户Paul ,但我想知道是否以管理员身份运行或不影响事情。
System.out.println("Path Exists(1):"+Files.exists(path));
System.out.println("Path Exist(2) :"+path.toFile().exists());
gives 给
Path Exists(1):false
Path Exist(2) :true
Also 也
System.out.println("Path readable(3) :"+Files.isReadable(path));
System.out.println("Path readable(4):"+path.toFile().canRead());
works in same way giving 以同样的方式给予
Path readable(3) :false
Path readable(4):true
Permissions output 权限输出
File C:\Code\jthink\opensrc\jaudiotagger\testdata\test157.dsf permissions
owner:PCLAPTOP\Paul
NT AUTHORITY\SYSTEM:READ_DATA/WRITE_DATA/APPEND_DATA/READ_NAMED_ATTRS/WRITE_NAMED_ATTRS/EXECUTE/DELETE_CHILD/READ_ATTRIBUTES/WRITE_ATTRIBUTES/DELETE/READ_ACL/WRITE_ACL/WRITE_OWNER/SYNCHRONIZE:DENY
BUILTIN\Administrators:READ_DATA/WRITE_DATA/APPEND_DATA/READ_NAMED_ATTRS/WRITE_NAMED_ATTRS/EXECUTE/DELETE_CHILD/READ_ATTRIBUTES/WRITE_ATTRIBUTES/DELETE/READ_ACL/WRITE_ACL/WRITE_OWNER/SYNCHRONIZE:DENY
BUILTIN\Administrators:READ_DATA/WRITE_DATA/APPEND_DATA/READ_NAMED_ATTRS/WRITE_NAMED_ATTRS/EXECUTE/DELETE_CHILD/READ_ATTRIBUTES/WRITE_ATTRIBUTES/DELETE/READ_ACL/WRITE_ACL/WRITE_OWNER/SYNCHRONIZE:ALLOW
NT AUTHORITY\SYSTEM:READ_DATA/WRITE_DATA/APPEND_DATA/READ_NAMED_ATTRS/WRITE_NAMED_ATTRS/EXECUTE/DELETE_CHILD/READ_ATTRIBUTES/WRITE_ATTRIBUTES/DELETE/READ_ACL/WRITE_ACL/WRITE_OWNER/SYNCHRONIZE:ALLOW
BUILTIN\Users:READ_DATA/READ_NAMED_ATTRS/EXECUTE/READ_ATTRIBUTES/READ_ACL/SYNCHRONIZE:ALLOW
NT AUTHORITY\Authenticated Users:READ_DATA/WRITE_DATA/APPEND_DATA/READ_NAMED_ATTRS/WRITE_NAMED_ATTRS/EXECUTE/READ_ATTRIBUTES/WRITE_ATTRIBUTES/DELETE/READ_ACL/SYNCHRONIZE:ALLOW
c:\Code\jthink\opensrc\jaudiotagger>attrib C:\Code\jthink\opensrc\jaudiotagger\testdata\test157.dsf
A R C:\Code\jthink\opensrc\jaudiotagger\testdata\test157.dsf
Update I dont have a conclusion but thought this information could be useful. 更新我没有得出结论,但认为这些信息可能有用。
I was running code in IntelliJ IDE without the IDE Run program as Administrator option enabled, enabling this did then cause the Java application to also get the administrator privileges. 我在IntelliJ IDE中运行代码而没有启用IDE Run选项的IDE Run程序 ,启用此功能后,Java应用程序也获得了管理员权限。
Interesting for another file I didn't add any DENY privileges, I just disabled inherit permissions and remove READ permissions from all groups. 感兴趣的是另一个文件我没有添加任何DENY权限,我只是禁用了继承权限并从所有组中删除了READ权限。 Then when I ran as user without run as admin enabled it could not read the file and also this code could not any output any information
然后,当我作为用户运行而没有运行管理员启用它无法读取文件,此代码也无法输出任何信息
AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
if (view != null)
{
sb.append("Owner:"+view.getOwner().getName()+"**");
for (AclEntry acl : view.getAcl())
{
sb.append(acl.principal()+"**");
for(AclEntryPermission aep:acl.permissions())
{
sb.append(aep.toString() + "**");
}
}
}
but when I run with Run program as adminstrator enabled it still couldnt read the file, but the above code did now output some of the permissions as follows: 但当我运行Run程序作为管理员启用它仍然无法读取文件,但上面的代码现在输出一些权限,如下所示:
Owner:BUILTIN\\Administrators
业主:BUILTIN \\管理员
NT AUTHORITY\\SYSTEM:WRITE_DATA/APPEND_DATA/WRITE_NAMED_ATTRS/WRITE_ATTRIBUTES/SYNCHRONIZE:ALLOW PCLAPTOP\\Paul:WRITE_DATA/APPEND_DATA/WRITE_NAMED_ATTRS/WRITE_ATTRIBUTES/SYNCHRONIZE:ALLOW BUILTIN\\Administrators:WRITE_DATA/APPEND_DATA/WRITE_NAMED_ATTRS/WRITE_ATTRIBUTES/SYNCHRONIZE:ALLOW
NT AUTHORITY \\ SYSTEM:WRITE_DATA / APPEND_DATA / WRITE_NAMED_ATTRS / WRITE_ATTRIBUTES / SYNCHRONIZE:ALLOW PCLAPTOP \\ Paul:WRITE_DATA / APPEND_DATA / WRITE_NAMED_ATTRS / WRITE_ATTRIBUTES / SYNCHRONIZE:ALLOW BUILTIN \\ Administrators:WRITE_DATA / APPEND_DATA / WRITE_NAMED_ATTRS / WRITE_ATTRIBUTES / SYNCHRONIZE:ALLOW
as you can see even though Administrators
do not have READ
or READ PERMISSIONS
options they can output the permissions whereas before they couldn't, perhaps due to BUILTIN/Administraor being returned as owner. 正如您所见,即使
Administrators
没有READ
或READ PERMISSIONS
选项,他们也可以输出权限,而在他们不能之前,可能是由于BUILTIN / Administraor作为所有者返回。
Try reading this: https://docs.oracle.com/javase/tutorial/essential/io/check.html 请尝试阅读: https : //docs.oracle.com/javase/tutorial/essential/io/check.html
It states that, Files.exists(path)
returning false does not mean that it does not exist, so yeah it would seem there is a permission problem. 它声明,
Files.exists(path)
返回false并不意味着它不存在,所以是的,似乎存在权限问题。 Try the Files.notExists(path)
as well and see what it returns. 尝试
Files.notExists(path)
并查看它返回的内容。 If it is false it means that it can not be determined whether the file exists, but if it returns true, there is probably some problem in your code. 如果为false,则表示无法确定文件是否存在,但如果返回true,则代码中可能存在一些问题。
Try running your file from the command line instead of netbeans. 尝试从命令行而不是netbeans运行您的文件。 If you don't know how to do this you can just search google, there is tons of stuff on this, but basically what you want to do is to compile the .java file with
javac myfile.java
and then run it with java myfile
. 如果你不知道如何做到这一点,你可以只搜索谷歌,这里有很多东西,但基本上你想要做的是用
javac myfile.java
编译.java文件然后用java myfile
运行它。 Do this with a normal command prompt and one you open as administrator and see what you get. 使用正常的命令提示符执行此操作,并以管理员身份打开并查看所获得的内容。
Theses are two different methods: Files.exists() and path.toFile().exists(). 这些是两种不同的方法:Files.exists()和path.toFile()。exists()。
Files.exists() defines that file denoted by this abstract pathname exists. Files.exists()定义由此抽象路径名表示的文件存在。 In other words that file exists and user has READ access to it.
换句话说,该文件存在,并且用户具有READ访问权限。
path.toFile().exists() indicates the file exists then there is no guarantee that a subsequence access will succeed. path.toFile()。exists()表示文件存在,然后无法保证子序列访问将成功。 In other words file exist without checking that user has READ access to it.
换句话说,文件存在而不检查用户是否具有READ访问权限。
It really depends on user which runs the program. 它真的取决于运行该程序的用户。 When you work under your ID (Paul) it works fine.
当你按照你的身份(保罗)工作时,它运作正常。 Especially in command line where you gan you ATTRIB command.
特别是在命令行中,你可以使用ATTRIB命令。
However, when you use some other application to run your code it depends on the system configuration. 但是,当您使用其他应用程序运行代码时,它取决于系统配置。 Run this ATTRIB or similar command inside your application and you will see.
在您的应用程序中运行此ATTRIB或类似命令,您将看到。
I think you run some web site under IIS. 我想你在IIS下运行一些网站。 This way is usually configured for lowest level user in the system with almost no rights to prevent security breaks.
这种方式通常配置为系统中的最低级别用户,几乎没有权限来防止安全中断。 Usually it is everyone or NT AUTHORITY.
通常是每个人或NT AUTHORITY。 As I can see this particular access has no rights to read your file
我可以看到此特定访问权限无权读取您的文件
NT AUTHORITY\\SYSTEM:READ_DATA/...:DENY
NT AUTHORITY \\ SYSTEM:READ_DATA / ...:DENY
Naturally you have 2 different answers - FALSE: user which ID is used by running application cannot read this file, TRUE: file physically exist. 当然,您有两个不同的答案 - FALSE:运行应用程序使用哪个ID的用户无法读取此文件,TRUE:文件实际存在。
Change running ID for your application or grant READ access to everyone for this particular file including all directories in its path and you will have the same result in this two methods which check different meanings. 更改应用程序的运行ID或为此特定文件(包括其路径中的所有目录)的每个人授予READ访问权限,这两种检查不同含义的方法将产生相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.