File类File [] listFiles(FilenameFilter fnf) (File Class File[] listFiles(FilenameFilter fnf))
This method is available in package java.io.File.listFiles(FilenameFilter fnf).
软件包java.io.File.listFiles(FilenameFilter fnf)中提供了此方法。
This method is used to return the names of all the files in the form of an array of files which is represented in the filepath.
此方法用于以文件路径形式返回的文件数组形式返回所有文件的名称。
The return type of this method is File[] i.e. It returns an array of files or directories which is represented in filepath if a 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(FilenameFilter fnf){
}
Parameter(s):
参数:
We pass only one object FilenameFilter as a parameter in the method and with the help of this argument, we can find files with filter(i.e we can find specific files).
我们仅传递一个对象FilenameFilter作为方法中的参数,借助此参数,我们可以找到带有filter的文件(即,可以找到特定的文件)。
Return value:
返回值:
The return type of this method is File[] i.e. it returns all file names in an array of the file 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 ToListSpecificFiles {
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
FilenameFilter fnf = new FilenameFilter() {
public boolean accept(File file, String name) {
return name.startsWith("H");
}
};
// By using list(fnf) returns all the files and directories
// which is represented in a file path if file path is a
// directory and all file or directories starts with 'J'.
File[] filelist = file.listFiles(fnf);
System.out.println("These are the name of files represented in a given directory whose name starts with H :" + file.getPath());
// 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 ToListSpecificFiles.java
D:\Programs>java ToListSpecificFiles
These are the name of files represented in a given directory whose name starts with H:C:\Users\computer clinic\OneDrive\Articles
How to add characters to a string in Java.docx
How to add double quotes to a string in Java.docx
How to avoid NullPointerException in Java.docx
How to check if directory exists in java.docx
How to set path in Java for Windows.docx