Java IO

Make file hidden in Java example

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

How to check if 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

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

Note: Being hidden is platform dependent. If the file name starts with dot (“.”), for example .htaccess, 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 file hidden in Java?

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

Make files hidden in Unix systems (*nix systems)

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

Make files hidden in Windows systems

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

Output

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

Output

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

Tags
Join 1000+ fellow learners! Enter your email address below: