Java.io.File.listFiles() Method
Description
The java.io.File.listFiles(FileFilter filter) returns an array of abstract pathnames indicating the files and directories in the directory indicated by this abstract pathname that satisfy the specified filter.
Declaration
Following is the declaration for java.io.File.listFiles(FileFilter filter) method:
public File[] listFiles(FileFilter filter)
Parameters
filter - File filter
Return Value
The method returns an array of abstract pathnames indicating the files and directories in the directory indicated by this abstract pathname that satisfy the specified filter.
Exception
SecurityException -- If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file
Example
The following example shows the usage of java.io.File.listFiles(FileFilter filter) method.
package com.tutorialspoint;
import java.io.File;
import java.io.FileFilter;
public class FileDemo {
public static void main(String[] args) {
File f = null;
File[] paths;
try{
// create new file
f = new File("c:/test");
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile();
}
};
// returns pathnames for files and directory
paths = f.listFiles(filter);
// for each pathname in pathname array
for(File path:paths)
{
// prints file and directory paths
System.out.println(path);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
c:\test\child_test.txt c:\test\child_test.xlsx