開始使用免費開始

實作資料驗證

成功讀取市場資料檔後,你發現有些檔案可能是空的,或缺少重要欄位。在進行更複雜的分析之前,你需要實作基本的驗證檢查,確保這些資料檔可用。這能避免之後在資料處理流程中發生錯誤。

FilesPathPathsList 類別已為你匯入。

本練習屬於課程

Java 中的資料匯入

檢視課程

練習說明

  • 將檔案的所有行讀入清單。
  • 檢查檔案是否為空。
  • 驗證標頭是否包含預期的欄位名稱:DateTicker

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

public class SimpleDataValidation {
    public static void main(String[] args) {
        try {
            // Read all lines from the file
            Path path = Paths.____("sample_market_data.csv");
            List lines = Files.____(path);
            
            // Check if the file is empty
            System.out.println("File is empty: " + ____.____());
            
            // Verify the header contains expected column names
            if (!lines.isEmpty()) {
                String firstLine = lines.get(0);
                System.out.println("First line: " + firstLine);
                System.out.println("Contains 'Date': " + firstLine.____("____"));
                System.out.println("Contains 'Ticker': " + firstLine.contains("____"));
                System.out.println("Total lines: " + lines.size());
            }
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
        }
    }
}
編輯並執行程式碼