我需要在Windows和Linux上隐藏文件和文件夹。我知道加一个'.‘放在文件或文件夹的前面将使其在Linux上隐藏。如何在Windows上隐藏文件或文件夹?
发布于 2009-08-18 16:29:58
对于Java 6及更低版本,
您将需要使用本机调用,下面是windows的一种方法。
Runtime.getRuntime().exec("attrib +H myHiddenFile.java");您应该了解一些关于win32-api或Java Native的知识。
发布于 2009-08-18 19:15:31
您想要的功能是即将发布的Java7中的NIO.2的一个特性。
这里有一篇文章描述了它将如何用于你所需要的东西:Managing Metadata (File and File Store Attributes)。下面是一个使用DOS File Attributes的示例
Path file = ...;
try {
DosFileAttributes attr = Attributes.readDosFileAttributes(file);
System.out.println("isReadOnly is " + attr.isReadOnly());
System.out.println("isHidden is " + attr.isHidden());
System.out.println("isArchive is " + attr.isArchive());
System.out.println("isSystem is " + attr.isSystem());
} catch (IOException x) {
System.err.println("DOS file attributes not supported:" + x);
}可以使用DosFileAttributeView设置属性
考虑到这些事实,我怀疑在Java6或Java5中是否有一种标准而优雅的方法来实现这一点。
发布于 2010-06-13 00:04:24
Java 7可以通过以下方式隐藏DOS文件:
Path path = Paths.get("...");
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);较早的Java版本没有提供此功能的API。
上面的代码不会在非DOS文件系统上抛出异常。如果文件名以句点开头,则它在UNIX文件系统上将是隐藏的。
https://stackoverflow.com/questions/1294989
复制相似问题