如何使用 Java 讀取目錄中所有檔案的資料?
名為 File 的 java.io 包中的類表示系統中的檔案或目錄(路徑名)。此類提供各種方法來對檔案/目錄執行各種操作。
要獲取目錄中所有現有檔案的列表,此類提供了五種不同的方法來獲取特定資料夾中所有檔案的詳細資訊:
- 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 { //Creating a File object for directory File directoryPath = new File("D:\ExampleDirectory"); //List of all files and directories 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
讀取目錄中所有檔案的內容
要讀取特定目錄中所有檔案的內容,請使用上述方法將所有檔案的檔案物件作為陣列獲取,然後使用 Scanner 類及其方法讀取陣列中每個檔案物件的內容。
示例
import java.io.File; import java.io.IOException; import java.util.Scanner; public class ListOfFiles { public static void main(String args[]) throws IOException { //Creating a File object for directory File directoryPath = new File("D:\Demo"); //List of all files and directories 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()); //Instantiating the Scanner class 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
廣告