File类File [] listFiles(FileFilter ff) (File Class File[] listFiles(FileFilter ff))
This method is available in package java.io.File.listFiles(FileFilter fnf).
软件包java.io.File.listFiles(FileFilter fnf)中提供了此方法。
This method is used to return an array of filepath indicates files and directories in a given directory which is represented in the filepath that satisfy specified filter.
此方法用于返回文件路径数组,该数组指示给定目录中的文件和目录,这些文件和目录在满足指定过滤器的文件路径中表示。
The return type of this method is File[] i.e. It returns an array of filepath indicates files or directories which is represented in filepath if the given path is directory else return null.
此方法的返回类型为File [],即,它返回一个文件路径数组,该数组指示如果给定路径为目录,则在文件路径中表示的文件或目录,否则返回null。
This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.
如果未授予文件写入权限,则此方法可能会引发异常(即Security Exception)。
This method is overridable. The first method does not accept any parameter and the second method accept one parameter and Third method is also accept one parameter.
此方法是可重写的。 第一种方法不接受任何参数,第二种方法接受一个参数,第三种方法也接受一个参数。
Syntax:
句法:
File[] listFiles(FileFilter ff){
}
Parameter(s):
参数:
We pass only one object FileFilter as a parameter in the method and with the help of this argument, we can find files or directories with filter(i.e we can find specific files).
我们仅传递一个对象FileFilter作为方法中的参数,借助此参数,我们可以使用filter查找文件或目录(即可以找到特定的文件)。
Return value:
返回值:
The return type of this method is File[] i.e. it returns an array of filepath indicates all file names or directories which is represented in a filepath.
此方法的返回类型为File [],即它返回一个文件路径数组,该数组指示在文件路径中表示的所有文件名或目录。
Java程序演示listFiles()方法的示例 (Java program to demonstrate example of listFiles() method)
// import the File class because we will use File class methods
import java.io.*;
// import the Exception class because it may raise an
// exception when working with files
import java.lang.Exception;
public class ToListFiles {
public static void main(String[] args) {
try {
// Specify the path of file and we use double slashes to
// escape '\' character sequence for windows otherwise
// it will be considerable as url.
File file = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");
// Create a FilenameFilter object
FileFilter ff = new FileFilter() {
public boolean accept(File file) {
return file.getName().endsWith("pdf");
}
};
// By using list(ff) returns an array of file indicates all
// the files and directories which is represented in a file path
// if file path is a directory and all file or
// directories ends with 'pdf'.
File[] filelist = file.listFiles(ff);
System.out.println("These are the name of files represented in a given directory whose name ends with doc :");
// By using loop to traverse the filenames and directories
// in the given path .
for (int i = 0; i < filelist.length; i++)
System.out.println(filelist[i].getName());
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
Output
输出量
D:\Programs>javac ToListFiles.java
D:\Programs>java ToListFiles
These are the name of files represented in a given directory whose name ends with pdf :
DurgaSoft-Part-1_JavabynataraJ.pdf
DurgaSoft-Part-2_JavabynataraJ.pdf
翻译自: https://www.includehelp.com/java/file-class-file-listfiles-filefilter-ff-method-with-example.aspx