Intellij FileNotFoundException

선종우·2024년 4월 17일
0

오답노트

목록 보기
5/5
  • 개발 중에 텍스트 파일을 읽어들여 파싱해야할 일이 있었다. Intellij 환경에서 개발을 진행하던 중에 클래스 파일과 같은 위치에 존재하는 텍스트파일을 찾지 못하는 에러가 발생하였다.

  • 프로젝트 구조는 아래와 같았다

/FileReader
	/gradle
    /src
    	/main
        	/java
            	/kr
                	/intellij.txt
                    /Reader.java
                    /reflection.txt
			/kotlin
            /resources
    	/test                	
public class Reader {
    public static void main(String[] args) throws IOException {
        File reflectionFile = new File("./reflection.txt");
        try(BufferedReader br = new BufferedReader(new FileReader(reflectionFile))){
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }
    }
}
  • reflection.txt파일이 자바파일과 동일 경로에 있어서 문제가 없을 거라고 생각했는데 계속 FileNotFoundException이 발생했다.
  • 알아보니 파일이 존재하는 경로와 WorkingDirectory가 다르기 때문에 발생하는 문제였다. Intellij run configuration을 살펴보니 WorkingDirectroy가 프로젝트의 루트폴더인 /FileReader로 설정되어있었다. 해당 폴더에 파일을 위치해주니 정상적으로 파일을 읽을 수 있었다.

관련 링크

0개의 댓글