Java如何顯示目錄中的所有檔案?

2019-10-16 22:29:53

在Java程式中,如何顯示目錄中的所有檔案?

以下範例顯示如何使用File類的list()方法顯示目錄中包含的所有檔案。

package com.yiibai;

import java.io.*;

public class DisplayFilesInDirectory {
   public static void main(String[] args) {
      File dir = new File("F:\\worksp\\javaexamples\\java_directories");
      String[] children = dir.list();

      if (children == null) {
         System.out.println( "Either dir does not exist or is not a directory");
      } else { 
         for (int i=0; i< children.length; i++) {
            String filename = children[i];
            System.out.println(filename);
         }
      }
   }
}

執行上面範例程式碼,得到以下結果 -

.classpath
.project
bin
dir11
myfile.txt
src

範例-2

以下是顯示目錄中所有檔案的另一個範例,它顯示指定目錄及其子目錄中以.java為字尾的檔案。

package com.yiibai;

import java.io.File;

public class DisplayFilesInDirectory2 { 
   public static File folder = new File("F:\\worksp\\javaexamples\\java_directories");
   static String temp = "";

   public static void main(String[] args) {
      System.out.println("Reading files under the folder "+ folder.getAbsolutePath());
      listFilesForFolder(folder);
   } 
   public static void listFilesForFolder(final File folder) {
      for (final File fileEntry : folder.listFiles()) {
         if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry);
         } else {
            if (fileEntry.isFile()) {
               temp = fileEntry.getName();
               if ((temp.substring(temp.lastIndexOf('.') 
                  + 1, temp.length()).toLowerCase()).equals("java"))System.out.println(
                  "" + folder.getAbsolutePath()+ "\\" + fileEntry.getName());
            } 
         } 
      } 
   }
}

執行上面範例程式碼,得到以下結果 -

Reading files under the folder F:\worksp\javaexamples\java_directories
F:\worksp\javaexamples\java_directories\src\com\yiibai\CreateDirectories.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DeleteDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayRootDirectories.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\EmptyDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\FindCurrentDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchFileInDirectory.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles2.java
F:\worksp\javaexamples\java_directories\src\com\yiibai\TraversingDirectory.java