修改Java中的隐藏文件
我有一个用户下载的文件,然后我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我同意 Dolph 的观点,但是您也可以考虑使用隐藏文件的替代方法。首先,您现在依赖于(Windows)“attrib”命令。其次,仅仅因为文件被标记为隐藏并不意味着用户无法查看或修改它(我将我的机器设置为始终显示隐藏文件)。作为替代方案,您可以考虑使用标准目录位置和文件命名约定。例如,在 Windows 中,放置应用程序数据的标准位置是“应用程序数据”文件夹。您可以使用系统属性“user.home”找到此文件夹:
您可以使用它创建自己的应用程序数据文件夹:
类似地,在 *nix 环境中,应用程序通常将其数据保存在主目录中的 .xyz 目录中:
您可以查看属性 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":
You can use this create your own Application Data folder:
Similarly in *nix environments applications usually keep their data in a .xyz directory in the home directory:
You can look at the property os.name to determine what environment you are running on and construct a correct path based on that.
首先取消隐藏文件:
Unhide the file first: