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

问题描述

我的小型实用程序通过 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 doesnot continue with the processing (which might take a long time)

我的第一次尝试是 java.io.FilecanWrite() 方法.但这不起作用因为它处理目录条目本身而不是其内容.我至少见过可以重命名或删除但不能创建文件的 Windows XP 文件夹的一个实例在其中(由于权限).这实际上是我的测试用例.

My first attempt was the canWrite() method of java.io.File. But this does not worksince it deals with the directory entry itself and not its contents. I have seen at leastone instance of a Windows XP folder that can be renamed or deleted but no files may be createdin 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.

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

I suspect that there must be a better way for this but all solutions I have found so farwith 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 beforeactually 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.

另一种解决方案是将整个写入硬盘驱动器的代码放在 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.

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

08-14 00:01