开发者

How to make a folder hidden using java

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();
开发者_StackOverflow社区

It creates a directory which is not hidden.


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.


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.


under *nix you just rename the file so that

filename = ".".filename;


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.


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.


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();
          }

    }
}


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


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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜