Java File类File [] listFiles(FileFilter ff)方法(带示例)

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值