在创建文件内的文件之前检查目录中的写访问权限 [英] Checking for write access in a directory before creating files inside it

查看:104
本文介绍了在创建文件内的文件之前检查目录中的写访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小实用程序应用程序通过GUI文件选择器询问用户输出目录。
然后它在经过一些处理之后在这个输出目录中创建了很多文件。

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.

我需要检查应用程序是否具有写访问权限,以便通知用户并且
不会继续处理(这可能需要很长时间)

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)

我的第一次尝试是 canWrite()方法 java.io.File 。但是这不起作用
,因为它处理目录条目本身而不是它的内容。我已经看到至少
一个Windows XP文件夹的一个实例可以重命名或删除但是没有文件可以在其中创建
(因为权限)。这实际上是我的测试用例。

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.

我最终解决了以下解决方案

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.

我怀疑必须有更好的方法,但我到目前为止找到的所有解决方案都是
with安全管理器和东西处理Java Applet而不是独立的应用程序。
我错过了什么?

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?


实际写文件之前,建议在目录内检查文件访问的方法是什么?

What is the recommended way of checking for file access inside a directory before actually writing the files?

我使用的是Java 5.

I am using Java 5.

推荐答案

您可以检查文件权限,确保目录存在,并进行大量检查或找到一个能够检查所有内容的库但是(!)不是检查的最佳方法吗?如果您检查权限和文件系统更改...您将不得不更改您的代码。但是,尝试编写文件总是会告诉您是否可以编写文件。

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.

另一种解决方案是将整个写作放到硬盘上-drive代码,在try中。如果你不能写,整个部分将被跳过,你会在catch部分给出一条消息给用户反馈。

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.

这篇关于在创建文件内的文件之前检查目录中的写访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆