修改Java中的隐藏文件

发布于 2024-09-02 03:41:56 字数 328 浏览 10 评论 0原文

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

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

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

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

如果文件未隐藏,则此工作正常,但该文件需要隐藏,所以用户不会修改它。那么我该如何修改隐藏文件呢?在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)

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?

Thanks for your ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔梦 2024-09-09 03:41:56

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

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

您可以使用它创建自己的应用程序数据文件夹:

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

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

//*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 来确定您正在运行的环境并基于该环境构建正确的路径。

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

You can use this create your own Application Data folder:

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

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");

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

风和你 2024-09-09 03:41:56

首先取消隐藏文件:

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

Unhide the file first:

Runtime.getRuntime().exec("attrib -H myFile.txt");
                                  ^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文