【问题标题】:Hide a file using java使用java隐藏文件
【发布时间】:2014-07-22 11:58:20
【问题描述】:

我在我的 Swing 应用程序中使用 Java 中的 File() 创建了一个文件。我怎样才能让它隐藏在我的目录中。我听说过一些属性的变化,但在我初学者的角度上并不清楚。请帮忙。提前致谢。

隐藏完成感谢您的帮助。但是对隐藏文件的读写存在问题。请帮忙。隐藏文件在写/读时可以像普通文件一样对待吗?

【问题讨论】:

标签: java swing


【解决方案1】:

DOS 文件的纯 Java 7 方式:

Path file = Paths.get("fileToHide.dat");
Files.setAttribute(file, "dos:hidden", true);

http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

【讨论】:

  • 这就是你的意思:我的文件是example.xml。----所以这是代码:Path file = Paths.get("example.xml"); Files.setAttribute(file, "dos:hidden", true);
  • get 方法需要获取 example.xml 文件的完整路径。
  • 你能帮忙解决在隐藏文件上读写的问题吗?隐藏文件后出现错误!
【解决方案2】:

从 JDK 1.7 开始就可以使用 Java 来完成。使用java.nio.file 包查看How to hide file from Java Program with Code Example

Path path = FileSystems.getDefault().getPath("directory", "hidden.txt");
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
    path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 2011-04-12
    • 2010-11-25
    • 1970-01-01
    相关资源
    最近更新 更多