
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to list the 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
- Related Questions & Answers
- How to list out the hidden files in a Directory using Java program?
- How to list all files in a directory using Java?
- How to get the list of jpg files in a directory in Java?
- Java program to List all files in a directory recursively
- How to list all files (only) from a directory using Java?
- Java program to List all files in a directory and nested sub-directory - Recursive approach
- How to enable the display of hidden files in a JFileChooser in Java?
- How to list down all the files available in a directory using C#?
- Java program to delete all the files in a directory recursively (only files)
- How do I list all files of a directory in Python?
- How to list non-hidden files and directories in windows using Python?
- How to display files/folders including hidden files/folders in PowerShell?
- How can I get the list of files in a directory using C/C++?
- Determine if File or Directory is hidden in Java
- Java program to merge contents of all the files in a directory
Advertisements