
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
List Hidden Files in a Directory in Java
The ListFiles() method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.
The File Filter interface is filter for the path names you can pass this as a parameter to the listFiles() method. This method filters the file names passed on the passed filter.
To get the hidden directories in a folder, implement a FileFilter which accepts only hidden directories, and pass it as a parameter to the listFiles() method.
Example
import java.io.File; import java.io.FileFilter; import java.io.IOException; public class Test{ public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\ExampleDirectory"); //Creating filter for directories files FileFilter fileFilter = new FileFilter(){ public boolean accept(File dir) { if (dir.isDirectory()&& dir.isHidden()) { return true; } else { return false; } } }; File[] list = directoryPath.listFiles(fileFilter); System.out.println("List of the jpeg files in the specified directory:"); for(File fileName : list) { System.out.println(fileName.getName()); System.out.println(fileName); } } }
Output
List of the jpeg files in the specified directory: hidden directory1 D:\ExampleDirectory\hidden directory1 hidden directory2 D:\ExampleDirectory\hidden directory2
We can also get the list of hidden files using the is Hidden() method of the Files class −
Example
import java.io.File; import java.io.IOException; import java.util.Arrays; import java.util.stream.Stream; public class Demo { public static void main(String[] args) throws IOException { File dir = new File("D:\ExampleDirectory"); File[] files = dir.listFiles(File::isHidden); Stream <File> fileStream = Arrays.stream(files); fileStream.forEach(file -> System.out.println(file.getName())); } }
Output
D:\ExampleDirectory\hidden directory1 D:\ExampleDirectory\hidden directory2
Advertisements