如何使用 Java 读取目录中所有文件的数据?

java 8object oriented programmingprogramming更新于 2025/6/26 22:37:17

java.io 包中的 File 类表示系统中的文件或目录(路径名)。此类提供了多种方法来对文件/目录执行各种操作。

为了获取目录中所有现有文件的列表,此类提供了五种不同的方法来获取特定文件夹中所有文件的详细信息 −

  • String[] list()
  • File[] listFiles()
  • String[] list(FilenameFilter filter)
  • File[] listFiles(FilenameFilter filter)
  • File[] listFiles(FileFilter filter)

ListFiles() 方法

此方法返回一个数组,其中包含当前 (File) 对象所表示的路径中所有文件(和目录)的对象(抽象路径)。

以下 Java 程序打印路径中所有文件的名称、路径和大小D:\ExampleDirectory.

示例

import java.io.File;
import java.io.IOException;
public class ListOfFiles {
   public static void main(String args[]) throws IOException {
      //为目录创建 File 对象
      File directoryPath = new File("D:\ExampleDirectory");
      //所有文件和目录的列表
      File filesList[] = directoryPath.listFiles();
      System.out.println("List of files and directories in the specified directory:");
      for(File file : filesList) {
         System.out.println("File name: "+file.getName());
         System.out.println("File path: "+file.getAbsolutePath());
         System.out.println("Size :"+file.getTotalSpace());
         System.out.println(" ");
      }
   }
}

输出

List of files and directories in the specified directory:
File name: SampleDirectory1
File path: D:\ExampleDirectory\SampleDirectory1
Size :262538260480
File name: SampleDirectory2
File path: D:\ExampleDirectory\SampleDirectory2
Size :262538260480
File name: SampleFile1.txt
File path: D:\ExampleDirectory\SampleFile1.txt
Size :262538260480
File name: SampleFile2.txt
File path: D:\ExampleDirectory\SampleFile2.txt
Size :262538260480
File name: SapmleFile3.txt
File path: D:\ExampleDirectory\SapmleFile3.txt
Size :262538260480

读取目录中所有文件的内容

要读取特定目录中所有文件的内容,请使用上述方法获取该目录中所有文件的 File 对象作为数组,然后使用 Scanner 类及其方法读取数组中每个文件对象的内容。

示例

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class ListOfFiles {
   public static void main(String args[]) throws IOException {
      //为目录创建 File 对象
      File directoryPath = new File("D:\Demo");
      //所有文件和目录的列表
      File filesList[] = directoryPath.listFiles();
      System.out.println("List of files and directories in the specified directory:");
      Scanner sc = null;
      for(File file : filesList) {
         System.out.println("File name: "+file.getName());
         System.out.println("File path: "+file.getAbsolutePath());
         System.out.println("Size :"+file.getTotalSpace());
         //实例化扫描仪类
         sc= new Scanner(file);
         String input;
         StringBuffer sb = new StringBuffer();
         while (sc.hasNextLine()) {
            input = sc.nextLine();
            sb.append(input+" ");
         }
         System.out.println("Contents of the file: "+sb.toString());
         System.out.println(" ");
      }
   }
}

输出

List of files and directories in the specified directory:
File name: samplefile1.txt
File path: D:\Demo\samplefile1.txt
Size :262538260480
Contents of the file: Contents of the sample file 1
File name: samplefile2.txt
File path: D:\Demo\samplefile2.txt
Size :262538260480
Contents of the file: Contents of the sample file 2
File name: samplefile3.txt
File path: D:\Demo\samplefile3.txt
Size :262538260480
Contents of the file: Contents of the sample file 3
File name: samplefile4.txt
File path: D:\Demo\samplefile4.txt
Size :262538260480
Contents of the file: Contents of the sample file 4
File name: samplefile5.txt
File path: D:\Demo\samplefile5.txt
Size :262538260480
Contents of the file: Contents of the sample file 5

相关文章