tags:

views:

506

answers:

5

Hi, I want to create a hidden folder using java application. That program should work across platform. So How to write a program which can create hidden folder.

I have tried using

File newFile = new File("myfile");
newFile.mkdir();

It creates a directory which is not hidden.

+3  A: 

To make a file or directory hidden under Unix, its name needs to start with a period (.).

To make a file hidden under Windows, you need to set the 'hidden' bit in its attributes. The Java standard library doesn't offer this capability (though there is a file.isHidden() method), and I don't offhand know any tool that does.

Carl Smotricz
+3  A: 

under *nix you just rename the file so that

filename = ".".filename;
+8  A: 

The concept of hidden files/folders is very OS-specific and not accessible via the Java API.

In Linux, files and folders whose name begins with a dot are hidden per default in many programs - doing that is easy.

In Windows, "hidden" is a special flag stored in the file system. There is no Java API for changing it; you can use Runtime.exec() to run the attrib command.

Michael Borgwardt
+2  A: 

You could use some form of a factory pattern for your crossplatforming needs. But what everyone else said. I'm afraid you can't quite make it plop out with one line of code, as I'm can just feel you want it to. My condolences.

piggles
+1  A: 

that's OS job (and you are OS boss of course ). But you can execute attrib (Windows) command and tell OS(Windows) that you wanna make a folder "hidden".

public class Main {

    public static void main(String[] args) {
        try
        {            
            Runtime rt = Runtime.getRuntime();
            //put your directory path instead of your_directory_path
            Process proc = rt.exec("attrib -s -h -r your_directory_path"); 
            int exitVal = proc.exitValue();
        } catch (Throwable t)
          {
            t.printStackTrace();
          }

    }
}
Michel Kogan