我需要隐藏在Windows和Linux的文件和文件夹。 我知道,附加一个“” 到一个文件或文件夹的前面会隐藏它在Linux上。 如何使隐藏在Windows文件或文件夹?
Answer 1:
对于Java 6及以下,
您将需要使用本地通话,这里是窗口中的一个途径
Runtime.getRuntime().exec("attrib +H myHiddenFile.java");
你应该学习一些关于Win32的API或Java本机。
Answer 2:
你想要的功能是在即将到来的Java 7 NIO.2的特性。
下面是描述它如何将用于您所需要的一篇文章: 管理元数据(文件和文件存储的属性) 。 还有用一个例子DOS文件属性 :
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
考虑到这些因素,我怀疑有实现这个Java 6中或Java 5的标准和优雅的方式。
Answer 3:
Java 7中可以隐藏一个DOS文件是这样的:
Path path = ...;
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}
早期的Java-S不能。
上面的代码不会抛出非DOS文件系统异常。 如果文件名以句点开头,那么它也将在UNIX文件系统隐藏。
Answer 4:
这是我使用:
void hide(File src) throws InterruptedException, IOException {
// win32 command line variant
Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
p.waitFor(); // p.waitFor() important, so that the file really appears as hidden immediately after function exit.
}
Answer 5:
在Windows上,使用Java NIO,文件
Path path = Paths.get(..); //< input target path
Files.write(path, data_byte, StandardOpenOption.CREATE_NEW); //< if file not exist, create
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS); //< set hidden attribute
Answer 6:
这里是一个隐藏在Windows上的任意文件的完全可编译Java 7的代码示例。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;
class A {
public static void main(String[] args) throws Exception
{
//locate the full path to the file e.g. c:\a\b\Log.txt
Path p = Paths.get("c:\\a\\b\\Log.txt");
//link file to DosFileAttributes
DosFileAttributes dos = Files.readAttributes(p, DosFileAttributes.class);
//hide the Log file
Files.setAttribute(p, "dos:hidden", true);
System.out.println(dos.isHidden());
}
}
要检查该文件是隐藏的。 对有问题的文件上单击右键,并且有问题的文件是真正隐藏在法院执行后,你会看到。
Answer 7:
String cmd1[] = {"attrib","+h",file/folder path};
Runtime.getRuntime().exec(cmd1);
使用此代码,它可能会解决你的问题
文章来源: Make a File/Folder Hidden on Windows with Java