How to find and open the Hidden files in a Directory using Java
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:
- Creation of a new file.
- Opening an existing file.
- Reading from a file.
- Writing to a file.
- Moving to a specific location in a file.
- 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:
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:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.