1. 程式人生 > 其它 >Java基礎 第四節 第十課

Java基礎 第四節 第十課

技術標籤:# Java 基礎第四節

綜合案例

檔案搜做

搜尋 C:/Users/Windows/Desktop 中的 .java 檔案.

分析

  1. 目錄搜尋, 無法判斷多少級目錄, 所以使用遞迴, 遍歷所有目錄
  2. 遍歷目錄時, 獲取的子檔案, 通過檔名稱, 判斷是否符合條件

程式碼展示

import java.io.File;

public class Test {
    public static void main(String[] args) {
        // 建立File物件
        File dir = new File("C:/Users/Windows/Desktop");
        // 呼叫列印目錄方法
        printDir(dir);
    }

    private static void printDir(File dir) {
        // 獲取子檔案和目錄
        File[] files = dir.listFiles();

        // 迴圈列印
        for (File file : files) {
            if (file.isFile()) {
                // 是檔案, 判斷出檔名並輸出檔案絕對路徑
                if (file.getName().endsWith(".java")) {
                    System.out.println("檔名: " + file.getAbsolutePath());
                }
            } else {
                // 是目錄, 繼續變數, 形成遞迴
                printDir(file);
            }
        }
    }
}

輸出結果:

檔名: C:\Users\Windows\Desktop\Amherst\LAB 4\AllEqual.java
檔名: C:\Users\Windows\Desktop\Amherst\LAB 4\Stars.java
檔名: C:\Users\Windows\Desktop\Amherst\LAB 5\Code-Student\NestedLoop.java
檔名: C:\Users\Windows\Desktop\Amherst\LAB 5\Code-Student\Purchase.java
檔名: C:\Users\Windows\Desktop\Amherst\LAB1_STUDENT\MyFirstProgram.java
檔名: C:\Users\Windows\Desktop\Amherst\LAB3 Code\Arithmeticx.java
檔名: C:\Users\Windows\Desktop\Amherst\LAB3 Code\FahrenheitToCelsiusx.java
檔名: C:\Users\Windows\Desktop\Amherst\LAB3 Code\MPGx.java

檔案過濾器優化

java.io.FileFilter是一個介面, 是 File 的過濾器. 該介面的物件可以傳遞給 FIle 類的 listFiles(FileFilter) 作為引數, 介面中只有一個方法:

boolean accept(File pathname) :
測試 pathname 是否應該包含在當前 File 目錄中, 符合則返回 true

分析

  1. 介面作為引數, 需要傳遞子類物件, 重寫其中方法. 我們選擇匿名內部類方式, 比較簡單
  2. accept 方法, 引數為 FIle, 表示當前 File 下所有的子檔案和子目錄. 保留住則返回 true, 過濾掉則返回 false (保留規則: 要麼是 .java 檔案, 要麼是目錄, 用於繼續遍歷)
  3. 通過過濾器的作用, listFiles(FileFilter) 返回陣列元素中, 子檔案物件都是符合條件的, 可以直接列印

程式碼展示

import java.io.File;
import java.io.FileFilter;

public class Test {
    public static void main(String[] args) {
        File dir = new File("C:/Users/Windows/Desktop");
        printDir2(dir);
    }

    private static void printDir2(File dir) {
        // 匿名內部類方式, 建立過濾器子類物件
        File[] files = dir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith(".java") || pathname.isDirectory();
            }
        });
        for (File file : files) {
            if (file.isFile()) {
                System.out.println("檔名: " + file.getAbsolutePath());
            } else {
                printDir2(file);
            }
        }
    }
}

Lambda 優化

分析

分析: FileFilter 是隻有一個方法的介面, 因此可以用 Lambda 表示式簡寫.

Lambda 格式:

()‐>{ }

程式碼展示

import java.io.File;

public class Test {
    public static void main(String[] args) {
        File dir = new File("C:/Users/Windows/Desktop");
        printDir3(dir);
    }

    private static void printDir3(File dir) {
        // Lambda 改寫
        File[] files = dir.listFiles((a) -> a.getName().endsWith(".java")||a.isDirectory());
        
        // 迴圈列印
        for(File file:files){
            if (file.isFile()){
                System.out.println("檔名: " + file.getAbsolutePath());
            }else{
                printDir3(file);
            }
        }
    }
}