当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java File listFiles()用法及代码示例


listFiles()方法是File类的一部分。如果路径名是目录,则该函数返回一个File数组,表示给定抽象路径名中的文件,否则返回null。该函数是重载函数。其中一个函数没有任何参数,第二个函数将FilenameFilter对象作为参数,第三个函数将FileFilter对象作为参数

函数签名:

public File[] listFiles()
public File[] listFiles(FilenameFilter f)
public File[] listFiles(FileFilter f)

函数语法:


file.listFiles()
file.listFiles(filter)

参数:该函数是重载函数

  • 该函数之一没有任何参数,
  • 第二个函数将FilenameFilter对象作为参数,
  • 第三个函数将FileFilter对象作为参数

返回值:该函数返回一个File数组,如果文件对象是一个文件,则返回null值。

异常:如果不允许该函数对该文件进行读取访问,则此方法将引发Security Exception

下面的程序将说明listFiles()函数的用法

范例1:我们将尝试查找给定目录中的所有文件和目录

// Java program to demonstrate the 
// use of listFiles() function 
  
import java.io.*; 
  
public class solution { 
    public static void main(String args[]) 
    { 
  
        // try-catch block to handle exceptions 
        try { 
  
            // Create a file object 
            File f = new File("f:\\program"); 
  
            // Get all the names of the files present 
            // in the given directory 
            File[] files = f.listFiles(); 
  
            System.out.println("Files are:"); 
  
            // Display the names of the files 
            for (int i = 0; i < files.length; i++) { 
                System.out.println(files[i].getName()); 
            } 
        } 
        catch (Exception e) { 
            System.err.println(e.getMessage()); 
        } 
    } 
}

输出:

Files are:
1232.txt
1245.txt
5671.txt
program1

范例2:我们将尝试在给定目录中查找名称以“12”开头的所有文件和目录。

// Java program to demonstrate the 
// use of listFiles() function 
  
import java.io.*; 
  
public class solution { 
    public static void main(String args[]) 
    { 
  
        // try-catch block to handle exceptions 
        try { 
  
            // Create a file object 
            File f = new File("f:\\program"); 
  
            // Create a FilenameFilter 
            FilenameFilter filter = new FilenameFilter() { 
  
                public boolean accept(File f, String name) 
                { 
                    return name.startsWith("12"); 
                } 
            }; 
  
            // Get all the names of the files present 
            // in the given directory 
            // and whose names start with "12" 
            File[] files = f.listFiles(filter); 
  
            System.out.println("Files are:"); 
  
            // Display the names of the files 
            for (int i = 0; i < files.length; i++) { 
                System.out.println(files[i].getName()); 
            } 
        } 
        catch (Exception e) { 
            System.err.println(e.getMessage()); 
        } 
    } 
}

输出:

Files are:
1232.txt
1245.txt

范例3:我们将尝试查找给定目录中的所有文件和目录,它们都是文本文件

// Java program to demonstrate the 
// use of listFiles() function 
  
import java.io.*; 
  
public class solution { 
    public static void main(String args[]) 
    { 
  
        // try-catch block to handle exceptions 
        try { 
  
            // Create a file object 
            File f = new File("f:\\program"); 
  
            // Create a FileFilter 
            FileFilter filter = new FileFilter() { 
  
                public boolean accept(File f) 
                { 
                    return f.getName().endsWith("txt"); 
                } 
            }; 
  
            // Get all the names of the files present 
            // in the given directory 
            // which are text files 
            File[] files = f.listFiles(filter); 
  
            System.out.println("Files are:"); 
  
            // Display the names of the files 
            for (int i = 0; i < files.length; i++) { 
                System.out.println(files[i].getName()); 
            } 
        } 
        catch (Exception e) { 
            System.err.println(e.getMessage()); 
        } 
    } 
}

输出:

Files are:
1232.txt
1245.txt
5671.txt

这些程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的父文件



相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 File listFiles() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。