Make a File/Folder Hidden on Windows with Java

Kryten picture Kryten · Aug 18, 2009 · Viewed 44.7k times · Source

I need to make files and folders hidden on both Windows and Linux. I know that appending a '.' to the front of a file or folder will make it hidden on Linux. How do I make a file or folder hidden on Windows?

Answer

Marian picture Marian · Aug 18, 2009

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.