2016-09-10 9 views
0

텍스트 파일을 읽는 프로그램을 얻으 려하지만 프로젝트 디렉토리에 potato.txt 파일이 설정되어 있어도 FileNotFoundException이 발생합니다.스캐너로 Java에서 파일 읽기

import java.io.File; 
import java.util.Scanner; 

public static void main(String[] args) { 
    String potato = "potato.txt"; // assume this line can't be changed at all 

    Scanner scan = new Scanner(new File(potato)); // throws FileNotFoundException 

    while (scan.hasNextLine()) { 
     String line = scan.nextLine(); 
     System.out.println(line); 
    } 


} 

} 
+0

당신의 파일은 정확히 무엇입니까? – Carlton

+0

절대 경로를 사용해보십시오. – mdp

+0

프로젝트 구조 게시하시기 바랍니다 – Saravana

답변

0

과 같이 전체 경로를 사용해보십시오 : 또한

File file = new File("C:/temp/potato.txt"); //This is just an example path, look up your files path and put it here. 
Scanner scan = new Scanner(file); 

, (당신의 while -loop 후) 수행 스캐너를 종료하는 것을 잊지 마세요 :

scan.close(); 
+0

나는 그것을 설정하고 ("C :/Users/The Turd 9000/workspace/IOTutorial/potato.txt") 파일 경로를 설정했습니다. getabsolutepath(); 그리고 그것은 여전히 ​​나에게 스캐너 라인에서 같은 FileNotFoundException을주고있다. – houndz

+0

프로젝트 구조에서 편집하고 질문에서 전체 오류 메시지를 볼 수 있도록하시기 바랍니다. – Carlton