June 23, 2022

How to Make a File or Folder Hidden in Java

In this post we’ll see how to make a file or folder hidden in Java and also how to unhide a file in Java. Setting the attribute for making a file hidden is OS specific and there is no uniform way to do it across platforms using Java.

Hiding a file in Unix

In Unix or Linux OS by adding a "." in front of a file you can make it hidden, for that you can rename a file using a Java program so that new name is "." + fileName.

Check this post Renaming a File Using Java Program to see a Java program for renaming a file.

Hiding a file or directory in Windows

For making a file or folder hidden in windows there are two options.

  1. Before Java 7 you could only make a file hidden from a Java program by running the attrib command using the RunTime.getRuntime().exec() method (See example) or using ProcessBuilder class (See example).
  2. Java 7 onward you can use setAttribute() method of the Files class to set the hidden attribute of a given file. See example.

Making a file hidden using RunTime.getRuntime().exec() method in Java

By using RunTime.getRuntime().exec() method you can run windows command “attrib” to set hidden attribute for a file to make it hidden.

  • Runtime.getRuntime()- Returns the runtime object associated with the current Java application.
  • exec(String command)- Executes the specified string command in a separate process.
  • attrib- This command is used to sets or display the read-only, archive, system, and hidden attributes of a file or directory. For hiding/unhiding a file attributes are +H/-H
import java.io.File;
import java.io.IOException;

public class HideFile {
  public static void main(String[] args) {
    File file = new File("F:\\knpcode\\Parent\\Test.txt");
    setHiddenAttrib(file);
  }
	
  private static void setHiddenAttrib(File file) {
    try {
      // execute attrib command to set hide attribute
      Process p = Runtime.getRuntime().exec("attrib +H " + file.getPath());
      // for removing hide attribute
      //Process p = Runtime.getRuntime().exec("attrib -H " + file.getPath());
      p.waitFor(); 
      if(file.isHidden()) {
        System.out.println(file.getName() + " hidden attribute is set to true");
      }else {
        System.out.println(file.getName() + " hidden attribute not set to true");
      }
    } catch (IOException | InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

If you want to remove hide attribute for a file then you can use the following command with Runtime.getRuntime().exec method-

"attrib -H " + file.getPath()

As you can see after running the code hidden attribute is set to true for the passed file.

file hide java program

Running attrib command using ProcessBuilder class

Same attrib command can also be run using ProcessBuilder class in Java. You can create a list with the command and the required arguments and then pass it to ProcessBuilder instance as command.

public class HideFile {
  public static void main(String[] args) {
    File file = new File("F:\\knpcode\\Parent\\Test.txt");
    setHiddenAttrib(file);
  }
	
  private static void setHiddenAttrib(File file) {
    try {
      List<String> cmdList = new ArrayList<String>();	    
      cmdList.add("attrib");
      cmdList.add("-H");
      cmdList.add(file.getPath());
      ProcessBuilder pb = new ProcessBuilder();
      pb.command(cmdList);
      Process p;		
      p = pb.start();
      p.waitFor();
    } catch (IOException | InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }         
  }
}

Making a file hidden using Files.setAttribute() method in Java

Files.setAttribute(Path path, String attribute, Object value, LinkOption... options)- Sets the value of a file attribute.

In the method passed parameters are-

  • path- the path to the file
  • attribute- the attribute to set
  • value- the attribute value
  • options- options indicating how symbolic links are handled

The attribute parameter identifies the attribute to be set and takes the form:

[view-name:]attribute-name
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.DosFileAttributes;

public class HideFile {
  public static void main(String[] args) {
    Path filePath = Paths.get("F:\\knpcode\\Parent\\Test.txt");
    setHiddenAttrib(filePath);
  }
	
  private static void setHiddenAttrib(Path filePath) {		
    try {
      DosFileAttributes attr = Files.readAttributes(filePath, DosFileAttributes.class);
      System.out.println(filePath.getFileName() + " Hidden attribute is " + attr.isHidden());
      Files.setAttribute(filePath, "dos:hidden", true);
      attr = Files.readAttributes(filePath, DosFileAttributes.class);
      System.out.println(filePath.getFileName() + " Hidden attribute is " + attr.isHidden());
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }
}

If you want to remove hide attribute for a file then you can pass false for the hidden attribute-

Files.setAttribute(filePath, "dos:hidden", false);

That's all for the topic How to Make a File or Folder Hidden in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment