2014-11-13 1 views
-4

정상적인 파일을 만들 수 없다는 문제가 있습니다! 이 내 코드입니다 :Eclipse에서 일반 File.txt를 만들 수 없습니다.

Attempting to read from file in: C:\Users\AHMED\workspace\Video\word.txt 
Exception in thread "main" java.io.FileNotFoundException: word.txt (The system cannot find the file specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.util.Scanner.<init>(Unknown Source) 
    at test.main(test.java:11) 
+0

합니다. 그 파일도 존재합니까? – Doorknob

+1

그리고이 오류에 대해 무엇을 이해하기가 어렵습니까? 존재하지 않는 파일을 읽으려고 시도하는 중이므로 예외가 아닌 다른 것을 기대하십니까? –

+0

글쎄, 나는 그것을 읽으려고해서 파일을 만드는 것이 "보통의"방법이 아니라고 생각한다. Java를 사용하여 파일을 만드는 방법에 대한 수천 개의 자습서를 검색하십시오. – Tom

답변

1

이 행 :

File myFile = new File("word.txt"); 

이 디스크에 새 파일을 생성하지 않습니다 나는이 오류가

public class test { 

    public static void main(String[] args) throws Exception { 
     File myFile = new File("word.txt"); 
     System.out.println("Attempting to read from file in: "+myFile.getCanonicalPath()); 

     Scanner input = new Scanner(myFile); 
     String in = ""; 
     in = input.nextLine(); 
    } 

} 

. 단지 메모리에 File 객체를 생성합니다.

그래서 파일에서 읽으려고하면 (디스크에) 존재하지 않으므로 오류가 발생합니다.

실제로 디스크에 실제 파일을 만들려면 데이터를 기록, 또는 "접촉"파일에 의해 빈 파일을 작성해야합니다 :

관련 문제