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.
1 |
public boolean isHidden() |
This method returns true if the file is hidden, false if the file is visible.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.javacodeexamples.ioexamples; import java.io.File; public class HiddenFileExample { public static void main(String[] args) { //path of the file String strFilePath = "C:/dir_1/data.txt"; //create file object File file = new File(strFilePath); /* * To check if the file is hidden or visible * use isHidden method of the File class */ boolean isHidden = file.isHidden(); System.out.println("Is file hidden? " + isHidden); } } |
Output
1 |
Is file hidden? false |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package com.javacodeexamples.ioexamples; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; public class HiddenFileExample { public static void main(String[] args) throws IOException { String strFilePath = "C:/dir_1/data.txt"; File file = new File(strFilePath); if(file.isHidden()) System.out.println("File is hidden"); else System.out.println("File is visible"); /* * Make file hidden using NIO */ //get path Path path = Paths.get("C:/dir_1/data.txt"); //set hidden attribute Files.setAttribute(path, "dos:hidden", true, LinkOption.NOFOLLOW_LINKS); if(file.isHidden()) System.out.println("File is hidden"); else System.out.println("File is visible"); } } |
Output
1 2 |
File is visible File is hidden |
If you are using an older version of Java, you can still make the file hidden using the Runtime class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.javacodeexamples.ioexamples; import java.io.File; import java.io.IOException; public class HiddenFileExample { public static void main(String[] args) throws IOException, InterruptedException { String strFilePath = "C:/dir_1/data.txt"; File file = new File(strFilePath); /* * Use exec method of Runtime to invoke attrib DOS command * and set hidden attribute of the file. */ Process p = Runtime.getRuntime().exec("attrib +H " + strFilePath); //wait for the command to complete p.waitFor(); if(file.isHidden()) System.out.println("File is hidden"); else System.out.println("File is visible"); } } |
Output
1 |
File is hidden |
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.
But Can You Tell Me how to make the folder visible again with first code block(Java 7
)
Hi,
Files.setAttribute(path, “dos:hidden”, false);
should do it. The last parameter is false.
Thanks for sharing
but I am using the above sample in eclipse and os is IOS due that I am not able to run the code
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.