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

2019-10-16 22:29:55

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

以下範例顯示如何顯示一個目錄中包含的所有目錄,為File類的list()方法建立一個過濾器。

package com.yiibai;

import java.io.*;

public class DisplayDirectories {
    public static void main(String[] args) {
        File dir = new File("F:\\worksp\\javaexamples\\java_directories");
        File[] files = dir.listFiles();
        FileFilter fileFilter = new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            }
        };
        files = dir.listFiles(fileFilter);
        System.out.println(files.length +" directories found.");

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

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

3 directories found.
F:\worksp\javaexamples\java_directories\bin
F:\worksp\javaexamples\java_directories\dir11
F:\worksp\javaexamples\java_directories\src

範例-2

以下是在Java中顯示一個目錄中的所有目錄的另一個範例 -

package com.yiibai;

import java.io.File;
import java.io.IOException;

public class DisplayDirectories2 {
    public static void main(String[] args) {
        File currentDir = new File("F:\\worksp\\javaexamples\\java_directories\\src");
        displayDirectoryContents(currentDir);
    }

    public static void displayDirectoryContents(File dir) {
        try {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.println("--directory:" + file.getCanonicalPath());
                    displayDirectoryContents(file);
                } else {
                    System.out.println("----file:" + file.getCanonicalPath());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

--directory:F:\worksp\javaexamples\java_directories\src\com
--directory:F:\worksp\javaexamples\java_directories\src\com\yiibai
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\CreateDirectories.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DeleteDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryHierarchy2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectoryModificationTime2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DirectorySize2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayDirectories.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayDirectories2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayFilesInDirectory2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\DisplayRootDirectories.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\EmptyDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\FindCurrentDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\HiddenDirectory2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\ParentDirectory2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchFileInDirectory.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\SearchingFiles2.java
----file:F:\worksp\javaexamples\java_directories\src\com\yiibai\TraversingDirectory.java