用Java修改隐藏文件 [英] Modify a hidden file in Java

查看:246
本文介绍了用Java修改隐藏文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户下载的文件然后我在java中执行命令来隐藏文件:

I have a file that user downloads and then I execute a command within java to hide the file:

Runtime.getRuntime().exec("attrib +H myFile.txt");

现在我需要访问该隐藏文件,但我得到了

Now later I need to access that hidden file but I'm getting

java.io.FileNotFoundException: myFile.txt (Access is denied)

如果文件未被隐藏,则此方法有效,但文件需要隐藏,因此用户不会修改它。那么我该如何修改隐藏文件呢?有没有办法在Java中做到这一点?

This works if file is not hidden, but the file needs to be hidden so user wont modify it. So how am I supposed to modify the hidden file? Is there any way to do this in Java?

感谢您的想法。

推荐答案

我同意Dolph,但您也可以考虑使用隐藏文件的替代方法。首先,您现在依赖于(Windows)attrib命令。其次,仅仅因为文件被标记为隐藏并不意味着用户无法看到或修改它(我将我的机器设置为始终显示隐藏文件)。作为替代方案,您可以考虑使用标准目录位置和filenaming约定。例如,在Windows中,放置应用程序数据的标准位置位于应用程序数据文件夹中。您可以使用系统属性user.home找到此文件夹:

I agree with Dolph, however you might also consider alternatives to using hidden files. The first is you are now dependent on the (Windows) "attrib" command. Secondly, just because a file is marked as hidden does not mean the user can not see or modify it (I have my machine set to always display hidden files). As an alternative you might consider using standard directory locations and filenaming conventions. For example in Windows a standard location to put your application data is in the folder "Application Data". You can find this folder using the System property "user.home":

System.out.println(System.getProperty("user.home"));
//prints out something like C:\Documents And Settings\smithj

你可以使用它创建自己的Application Data文件夹:

You can use this create your own Application Data folder:

//For Windows
File appDataDir = new File(System.getProperty("user.home"), "Application Data\\MyWidgetData");

同样在* nix环境中,应用程序通常将其数据保存在主目录中的.xyz目录中:

Similarly in *nix environments applications usually keep their data in a .xyz directory in the home directory:

//*nix OSes
System.out.println(System.getProperty("user.home"));
//prints out something like /user/home/smithj
File appDataDir = new File(System.getProperty("user.home"), ".MyWidgetData");

您可以查看属性os.name以确定您正在运行的环境并构造正确的基于此的路径。

You can look at the property os.name to determine what environment you are running on and construct a correct path based on that.

这篇关于用Java修改隐藏文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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