Skip to content

Make file hidden in Java example

Make file hidden in Java example shows how to make a file hidden in Java. The example also shows how to check if the file is hidden or visible.

How to check if the file is hidden in Java?

Java File class provides isHidden method which can be used to check if the file is hidden or visible.

This method returns true if the file is hidden, false if the file is visible.

Example

Output

The isHidden method may throw the SecurityException if there is no read access to the file.

Note: Being hidden is platform-dependent. If the file name starts with a dot (“.”), for example, it is considered to be hidden in Unix (*nix) systems. In Windows systems, hidden is an attribute of the file or directory.

How to make a file hidden in Java?

There is no unified way to make files hidden using Java. Hidden has different meanings for different operating systems.

Unix (*nix systems)

Simply renaming the files to have a dot (“.”) as a first character of the file name will make the file hidden in Unix systems.

Windows

There are a couple of ways to hide the files in Windows using Java. If you are using Java 7 or a later version, you can use NIO’s attribute as given in the below example.

Output

If you are using an older version of Java, you can still make the file hidden using the Runtime class as given below.

Output

Check out this example if you want to make the file read only. This example is a part of the Java File tutorial.

Please let me know your views in the comments section below.

About the author

5 comments

    1. Yes, above code is for windows only. As I have mentioned in the code, try renaming the file to have a dot (.) as first character in iOS.

Leave a Reply

Your email address will not be published.