Hi,
I need to make a file and/or folder hidden on both Windows and Linux. I know that appending a '.' to the front of a file/folder will make it hidden on Linux, but how do I do this on Windows?
Thanks
Hi,
I need to make a file and/or folder hidden on both Windows and Linux. I know that appending a '.' to the front of a file/folder will make it hidden on Linux, but how do I do this on Windows?
Thanks
You will need to use a native call, here is one way for windows
Runtime.getRuntime().exec("attrib +H myHiddenFile.java");
You should learn a bit about win32-api or Java Native.
The functionality that you desire is a feature of NIO.2 in the upcoming Java 7.
Here's an article describing how will it be used for what you need: Managing Metadata (File and File Store Attributes). There's an example with 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);
}
Setting attributes can be done using DosFileAttributeView
Considering these facts, I doubt that there's a standard and elegant way to accomplish that in Java 6 or Java 5.
this is what I use:
void hide(File src) throws InterruptedException, IOException {
// win32 command line variant
Process p = Runtime.getRuntime().exec("attrib +h " + src.getPath());
p.waitFor();
}
Java 7 can hide a DOS file this way:
Path path = ...;
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}
Earlier Java-s can't.
The above code will not throw an exception on non-DOS file-systems. If the name of the file starts with a period, then it will also be hidden on UNIX file-systems.