Open In App

How to find and open the Hidden files in a Directory using Java

Last Updated : 10 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisites: Java File Handling

So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file.

In this article, we will see how to open all the hidden files in the given path using Java

What is File Handling?
A file is a container which is used to store various types of information. Data is permanently stored in secondary memory by creating a file with a unique name. A file may consist of text, image or any other document.

Different Operations that can be performed on a file: There are various operations that can be performed on a file. They are:

  1. Creation of a new file.
  2. Opening an existing file.
  3. Reading from a file.
  4. Writing to a file.
  5. Moving to a specific location in a file.
  6. Closing a file.

Approach: All the above operations can be performed on the files which are visible when browsed through the directories. However, there may be cases where the files present in the directory are hidden and the data is securely stored in the hidden files. However, the files which are hidden can also be accessed using java. The IO package of java contains a special method named isHidden() whose return type is a boolean which returns true if the file is hidden and false if the file is not hidden. Apart from that, the java AWT package contains the Desktop class which has all the methods required to open, edit, browse the folders. Therefore, the idea to open the hidden files is to use the isHidden() method to check if the file is hidden or not and use open() method to open those hidden files.

Below is the implementation of the above approach:




// Java program to find and open
// all the hidden files in a
// directory
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
  
public class GFG {
  
    // Function to find and open
    // all the hidden files in a
    // directory
    public static void open(String path)
        throws IOException
    {
        // Creating the file object
        // for the path
        File file = new File(path);
  
        // List all the files in
        // the given path
        String[] ls = file.list();
  
        // Creating a file object
        // to access the files in
        // the given path
        File f;
  
        // Iterating through all the files
        for (int i = 0; i < ls.length; i++) {
  
            // Finding the path of the
            // file
            String npath = path + "/" + ls[i];
  
            // Accessing the file
            f = new File(npath);
  
            // Check whether there is any
            // hidden file
            if (f.isHidden()) {
  
                // Printing the hidden file
                System.out.println(
                    ls[i]
                    + "[" + npath + "]");
  
                // Open the hidden file using
                // the Desktop class
                Desktop d = Desktop.getDesktop();
                d.open(f);
  
                System.out.println(
                    ls[i] + " opened");
            }
        }
    }
  
    // Driver code
    public static void main(String[] args)
        throws IOException
    {
  
        String path = "D:";
  
        open(path);
    }
}


Note: The above code cannot be run on the online IDE.

Output: Here, a text file with some data in it has been hidden in the “D:” directory. The working of the above program is as follows:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads