Friday, January 13, 2012

How to create hidden file in Java- Example Tutorial

In last article we saw how to check hidden files in Java and now we will see how to hide file in Java or make hidden file in Java. As we said File API in Java doesn’t provide any method to make a file hidden in Java but still you can apply some quick tricks to hide files from Java program. Like in Unix environment any file whose names begin with dot (.) is hidden so you can name your  file starting with dot (.)  and your File will be hidden in Linux or Unix Environment. On Windows you need to directly execute DOS command as stated in how to execute shell commands from Java by using Runtime.getRuntime.exec(“command”) to make the file hidden in Windows. Here is an example of making file hidden in Windows using Java Program.

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

How to make a File hidden from Java Program

Now with JDK7 which introduced Automatic Resource Management , fork join framework  and String in Switch Case , you have a whole new set of File API in java.nio.file package  which provides lots of useful  File and Attributed related functionality like Path which encapsulate Path for any file.

Hide a File in Java5 with Example

how to hide file in java program with exampleHere is  complete code example you can use to make a File hidden from Java without using JDK7, disadvantage of this approach is that you are invoking shell command directly from Java code which makes it platform dependent and goes against java primary advantage of write once and read anywhere:

void setHiddenProperty(File file) throws InterruptedException, IOException {
    Process p = Runtime.getRuntime().exec("attrib +H " + file.getPath());

here call to p.waitFor() is optional but  calling waitFor() ensures that file will appear hidden as soon as function exit.

Now we will see code example to hide a file in Java7 using new java.nio.file package and new classes like “Path” which encapsulate file path in various operating system and can be used to  operate on files, directories, and other types of files in Java.

Code Example of making a file hidden in JDK7

Here is complete code example of hiding a File in windows from Java7 new features.

Path path = FileSystems.getDefault().getPath("directory", "hidden.txt");
Boolean hidden = path.getAttribute("dos:hidden", LinkOption.NOFOLLOW_LINKS);
if (hidden != null && !hidden) {
    path.setAttribute("dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);

That’s all on how to hide file in both Java5 and Java7 by using either Runtime.exec() or new java.nio.file package introduced in JDK7. let me know if you know any other way of making a file hidden from Java program and we can include that example here.

Further Learning
Java Fundamentals Part 1,2
Java Fundamentals: The Java Language
Core Java, Volume II--Advanced Features

Some other Java Tutorial you may like:


Mike said...

I like the JDK7 way to hide file in Java. it looks there is lot to explore in Java 7 with new FileSystem API.

Anonymous said...

How to hide file with spaces in name in java5's shell command way.

Post a Comment