기본 파일 리더 만들기
여러분은 산업 동향을 추적하는 스타트업의 데이터 분석가로 일하고 있어요. 팀은 매일 시장 데이터가 담긴 CSV 파일을 받아 처리해야 합니다. 데이터 파이프라인을 구축하는 첫 단계로, 처리에 앞서 이 파일을 읽고 존재 여부를 확인할 수 있는 신뢰할 만한 방법을 만들어야 합니다.
File, Path, Paths, Files 클래스는 미리 임포트되어 있습니다.
이 연습은 강의의 일부입니다
Java에서 데이터 가져오기
연습 안내
- 파일 경로를 사용해
File객체를 생성하세요. - 파일 존재 여부를 확인하세요.
- 파일 크기(바이트)를 가져오세요.
- 파일 경로에서
Path객체를 생성하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
public class BasicFileReaders {
public static void main(String[] args) {
String filePath = "sample_market_data.csv";
// Create a File object
File file = new File(____);
// Check if the file exists
System.out.println("File exists: " + ____.____());
// Get the file size in bytes
System.out.println("Size: " + ____.____() + " bytes");
try {
// Create a Path object from the file path
Path path = Paths.____(filePath);
List lines = Files.readAllLines(path);
System.out.println("Total lines: " + lines.size());
System.out.println("Header: " + lines.get(0));
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}