Java文件类String [] list(FilenameFilter fnf)方法,带示例
File Class String []列表(FilenameFilter fnf) (File Class String[] list(FilenameFilter fnf))
This method is available in package java.io.File.list(FilenameFilter fnf).
軟件包java.io.File.list(FilenameFilter fnf)中提供了此方法。
This method is used to return the names of all the files in the form of an array of strings which is represented in the filepath.
此方法用于以字符串?dāng)?shù)組的形式返回所有文件的名稱,這些字符串以文件路徑表示。
The return type of this method is String[] i.e. It returns an array of strings of all the files or directories which is represented in filepath if the given path is directory else return null.
此方法的返回類型為String [],即,如果給定路徑為目錄,則返回文件路徑中表示的所有文件或目錄的字符串?dāng)?shù)組,否則返回null。
This method may raise an exception( i.e. Security Exception) if the write access is not given to the file.
如果未授予文件寫入權(quán)限,則此方法可能會引發(fā)異常(即Security Exception)。
This method is overridable. The first method does not accept any parameter and the second method accept one parameter.
此方法是可重寫的。 第一種方法不接受任何參數(shù),第二種方法則接受一個參數(shù)。
Syntax:
句法:
String[] list(){}Parameter(s):
參數(shù):
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作為方法中的參數(shù),借助此參數(shù),我們可以找到帶有filter的文件(即,可以找到特定的文件)。
Return value:
返回值:
The return type of this method is String[] i.e. it returns all files names in an array of strings which is represented in a filepath.
此方法的返回類型為String [],即它以文件路徑表示的字符串?dāng)?shù)組形式返回所有文件名。
Java程序演示list()方法的示例 (Java program to demonstrate example of list() 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 objectFilenameFilter fnf = new FilenameFilter() {public boolean accept(File file, String name) {return name.startsWith("J");}};// 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'.String[] filelist = file.list(fnf);System.out.println("These are the name of files represented in a given directory :" + 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]);} catch (Exception e) {System.out.println("An error occurred.");e.printStackTrace();}} }Output
輸出量
D:\Programs>javac ToListSpecificFiles.javaD:\Programs>java ToListSpecificFiles These are the name of files represented in a given directory :C:\Users\computer clinic\OneDrive\Articles Java File Class canExecute().docx Java File Class canRead().docx Java File Class canWrite().docx翻譯自: https://www.includehelp.com/java/file-class-string-list-filenamefilter-fnf-method-with-example.aspx
總結(jié)
以上是生活随笔為你收集整理的Java文件类String [] list(FilenameFilter fnf)方法,带示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: chown –r mysql:mysql
- 下一篇: java美元兑换,(Java实现) 美元