데이터 유효성 검사 구현
시장 데이터 파일을 성공적으로 읽어 보니, 일부 파일은 비어 있거나 중요한 열이 빠져 있을 수 있다는 점을 발견했어요. 복잡한 분석을 진행하기 전에, 데이터 파일을 안전하게 사용할 수 있도록 기본 유효성 검사를 구현해야 합니다. 이렇게 하면 이후 데이터 처리 파이프라인에서 오류를 예방할 수 있어요.
Files, Path, Paths, List 클래스는 미리 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Java에서 데이터 가져오기
연습 안내
- 파일의 모든 줄을 읽어 목록에 담으세요.
- 파일이 비어 있는지 확인하세요.
- 헤더에 기대하는 열 이름
Date와Ticker가 포함되어 있는지 검증하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
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());
}
}
}