JAVA/JAVA 공부

[java] 컴파일 시점에 해당 경로에 파일 존재여부 확인하기

package javaProject;

import java.io.FileReader;

/*
 * 작성일자: 2021-08-26
 * 프로그램 설명: Exception 에 대한 예제
*/

public class ExceptionExam2 {

	public static void main(String[] args) {
		// FileNotFoundException
		String filename = "C:/test/Hello.txt";

		try {
			FileReader fr = new FileReader(filename);
		} catch (Exception e) { // FileNotFoundException도 가능함// 코드 가독성에는 있는게 더좋음
			System.out.println(e.getMessage());
		}
		System.out.println("end!");
	}

}
728x90
반응형