16

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.

Jørn Schou-Rode
  • 37,718
  • 15
  • 88
  • 122
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243

8 Answers8

30

If you're using Java 7 you can use the new java.nio.file.attribute package like so:

Path path = FileSystems.getDefault().getPath("/j", "sa");
Files.setAttribute(path, "dos:hidden", true);

See more info at http://download.oracle.com/javase/tutorial/essential/io/fileAttr.html

Or, if you're using an older version of Java and/or want to do it using Runtime, try this:

Process process = Runtime.getRuntime().exec("cmd.exe /C attrib -s -h -r your_path"); 

See more info on cmd and attrib.

Pål Brattberg
  • 4,568
  • 29
  • 40
15

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
  • 342,105
  • 78
  • 482
  • 720
  • 1
    While this works, I'd recommend you use Pal's Java 7 method mentioned below before resorting to the runtime hack (and check to make sure it's windows before doing the runtime hack) – David Welch Feb 05 '13 at 22:34
8

under *nix you just rename the file so that

filename = ".".filename;
user242294
  • 318
  • 1
  • 9
5

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
  • 66,391
  • 18
  • 125
  • 167
3

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.

Ritwik Bose
  • 5,889
  • 8
  • 32
  • 43
3

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 Gokan Khan
  • 2,525
  • 3
  • 30
  • 54
  • It is necessary to wait for the process to exit before taking its exit code. `proc.waitFor()` should be used instead of `proc.exitValue()`, as the latter causes an `IllegalThreadStateException: process has not exited` – Ondřej Bouda Oct 14 '12 at 16:29
0

Try following steps :

 1. make a folder with extension **.jad** and move your videos,photos, etc
    on that folder..
 2. now create same folder with extenson **.jar** (ex- if u create
    videos.jad then create videos.jar)
 3. finished .. Videos.jad will hide .. Delete the .jar  .jad will come
    again
Raja Simon
  • 10,126
  • 5
  • 43
  • 74
0

Exception in thread "main" java.io.IOException: Cannot run program "attrib": error=2, No such file or directory

In fact, the key to the question is what operating system you are using, MacOS and Windows have very different commands.

The commands provided online are all based on Windows, which is why your operation is invalid.

If you want to hide the folder on Windows, you can use the following code,

Runtime.getRuntime().exec("attrib +H " + dir.getAbsolutePath());

If you want to hide the folder on MacOS, you can use the following code,

Runtime.getRuntime().exec("chflags hidden " + dir.getAbsolutePath());