Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Wednesday, May 24, 2023

How to create a hidden file in Java- Example Tutorial

In the last article, we saw how to check hidden files in Java and now we will see how to hide files in Java or make hidden files 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 a Unix environment any file whose names begin with a dot (.) is hidden so you can name your file starting with a dot (.)  and your File will be hidden in Linux or Unix Environment.

On Windows, you need to directly execute the 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 files hidden in Windows using Java Program.

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

And, If you are new to the Java world then I also recommend you go through these online Java programming courses to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.

Using Runtime.exec()

How to create a hidden file in Java- Example Tutorial

Using New File IO

How to hide a file in Java

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 Files and Attributed related functionality like Path which encapsulates Path for any file.

How to Hide a File in Java 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, the 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 systems 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 a 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 files in Java 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 the Java program and we can include that example here.

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.

Anonymous said...

If your handy with CMD and java, anyone who is anyone knows that that way is no longer used...

To make ANY file hidden all you have to do is:

try {
Runtime.getRunTime().exec("attrib +h \""+file+"\"");
} catch (IOException e) {
//Ignore Error

Please if your gonna show how to make a file hidden IN JAVA, at least stay up-to-date...

Post a Comment