Java 在目录中创建文件之前检查目录中的写访问权限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1272130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking for write access in a directory before creating files inside it
提问by kazanaki
My small utility application asks the user for an output directory via a GUI file selector. Then it creates a lot of files in this output directory after some processing.
我的小型实用程序通过 GUI 文件选择器向用户询问输出目录。然后经过一些处理,它在这个输出目录中创建了很多文件。
I need to check if the application has write access so that it informs the user and does not continue with the processing (which might take a long time)
我需要检查应用程序是否具有写入权限,以便通知用户并且不继续处理(这可能需要很长时间)
My first attempt was the canWrite()method of java.io.File. But this does not work since it deals with the directory entry itself and not its contents. I have seen at least one instance of a Windows XP folder that can be renamed or deleted but no files may be created in it (because of permissions). This is actually my testcase.
我的第一次尝试是java.io.File的canWrite()方法。但这不起作用,因为它处理目录条目本身而不是其内容。我见过至少一个 Windows XP 文件夹的实例,它可以重命名或删除,但不能在其中创建任何文件(由于权限)。这实际上是我的测试用例。
I finally settled with the following solution
我终于解决了以下解决方案
//User places the input file in a directory and selects it from the GUI
//All output files will be created in the directory that contains the input file
File fileBrowse = chooser.getSelectedFile(); //chooser is a JFileChooser
File sample = new File(fileBrowse.getParent(),"empty.txt");
try
{
/*
* Create and delete a dummy file in order to check file permissions. Maybe
* there is a safer way for this check.
*/
sample.createNewFile();
sample.delete();
}
catch(IOException e)
{
//Error message shown to user. Operation is aborted
}
However this does not feel elegant to me since it just tries to actually create a file and checks if the operation succeeds.
然而,这对我来说并不优雅,因为它只是尝试实际创建一个文件并检查操作是否成功。
I suspect that there must be a better way for this but all solutions I have found so far with Security Managers and stuff deal with Java Applets and not standalone applications. Am I missing something?
我怀疑必须有更好的方法来解决这个问题,但到目前为止我发现的所有解决方案都涉及安全管理器和处理 Java 小程序而不是独立应用程序的东西。我错过了什么吗?
What is the recommended way of checking for file access inside a directory before actually writing the files?
在实际写入文件之前检查目录内文件访问的推荐方法是什么?
I am using Java 5.
我正在使用 Java 5。
采纳答案by Philippe Carriere
You could check the file permissions, make sure the directory exists, and do a lot of checking or find a library that does all that checking for you BUT (!)isn't the best way of checking to try ? If you check for permissions and the filesystem changes... you will have to change your code. But trying to write a file will ALWAYS tell you if you can write a file.
您可以检查文件权限,确保目录存在,并进行大量检查或找到一个可以为您完成所有检查的库, 但是(!)不是检查尝试的最佳方法吗?如果您检查权限并且文件系统发生变化……您将不得不更改您的代码。但是尝试写入文件总是会告诉您是否可以写入文件。
Your solution doesn't have to be the most elegant one. It's not a cheap hard coded patch or something ugly. It's just normal code. And it will always work. But if you don't like to see that check in the code just separate it by putting it in class which only goal is to check for the possibly of writing. In fact, you should put it in a utility class wheter you like the elegance or not.
您的解决方案不必是最优雅的。它不是廉价的硬编码补丁或丑陋的东西。这只是普通的代码。它会一直奏效。但是,如果您不喜欢在代码中看到该检查,只需将它放在类中即可将其分开,唯一的目标是检查编写的可能性。实际上,无论您喜欢优雅与否,都应该将其放在实用程序类中。
The other solution would be to place your whole writing-to-the-hard-drive code, in the try. And if you can't write, the whole part will be skipped and you give feedback to the user with a message in the catch part.
另一种解决方案是将整个写入硬盘驱动器的代码放在 try 中。如果你不会写,整个部分将被跳过,你在 catch 部分用一条消息向用户提供反馈。
回答by dfa
it doesn't works even if you invoke canWrite
on the final path?
即使您canWrite
在最终路径上调用它也不起作用?
File sample = new File(fileBrowse.getParent(),"empty.txt");
if (sample.canWrite()) {
doSomethingUseful(sample);
} else {
notifyUser();
}
回答by urmalp
you can use FilePermissionto get the details . I find one way where you need to implement SecurityManagerthe code is hereand here
您可以使用FilePermission获取详细信息。我找到了一种需要实现SecurityManager的方法,代码在这里和这里
回答by ElectronicBlacksmith
Using Java 1.8 I was able to use the following.
使用 Java 1.8 我能够使用以下内容。
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(Paths.get(destDir), LinkOption.NOFOLLOW_LINKS);
Assert.assertTrue("User did not have read permission.", permissions.contains(PosixFilePermission.OWNER_READ));
Assert.assertTrue("User did not have execute permission.", permissions.contains(PosixFilePermission.OWNER_EXECUTE));
Assert.assertTrue("User did not have write permission.", permissions.contains(PosixFilePermission.OWNER_WRITE));
Assert.assertFalse("Group did have read permission.", permissions.contains(PosixFilePermission.GROUP_READ));
Assert.assertFalse("Group did have execute permission.", permissions.contains(PosixFilePermission.GROUP_EXECUTE));
Assert.assertFalse("Group did have write permission.", permissions.contains(PosixFilePermission.GROUP_WRITE));
Assert.assertFalse("Others did have read permission.", permissions.contains(PosixFilePermission.OTHERS_READ));
Assert.assertFalse("Others did have execute permission.", permissions.contains(PosixFilePermission.OTHERS_EXECUTE));
Assert.assertFalse("Others did have write permission.", permissions.contains(PosixFilePermission.OTHERS_WRITE));