2014-09-26 61 views
0

嗨我想設置一個掃描儀來打印出文本文件的內容。這裏是我的代碼:Java找不到文件?

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

public class ScannerReadFile { 

    public static void main(String[] args) { 

     // Location of file to read 
     File file = new File("CardNative.java.txt"); 

     try 
     { 

       Scanner scanner = new Scanner(file); 

       while (scanner.hasNextLine()) 
       { 
        String line = scanner.nextLine(); 
        System.out.println(line); 
       } 
       scanner.close(); 
     } 
     catch (FileNotFoundException e) 
     { 
       e.printStackTrace(); 
     } 

    } 
} 

我已經在項目中創建了一個源文件夾,並將文本文件放在那裏。不過,我不斷收到此錯誤:

java.io.FileNotFoundException: CardNative.java.txt (The system cannot find the file specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(FileInputStream.java:120) 
    at java.util.Scanner.<init>(Scanner.java:636) 
    at ScannerReadFile.main(ScannerReadFile.java:14) 
+0

我懷疑,你只需要寫'CardNative.java' – 2014-09-26 09:22:20

+0

你想加載到類路徑上的文件是? – imrichardcole 2014-09-26 09:22:47

+0

不,它實際上是一個文本文件,全稱是CardNative.java.txt,這聽起來有點尷尬,但是作爲imrichardcole的全名 – mark16 2014-09-26 09:23:35

回答

2

您可以根據System.out.println(System.getProperty("user.dir"));看到什麼文件夾中的Java默認情況下,該文件看。
如果文件不存在,則必須指定文件的完整路徑。

+0

嗨,這是禍不單行,我發現路徑爲: c :\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt但是當我把File file = new File(「c:\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt」); c:\下劃線爲有錯誤: 無效轉義序列(有效的轉義序列爲\ b \ t \ n \ f \ r \「\'\\ – mark16 2014-09-26 09:38:46

+0

您需要將」\「更改爲」\\「路徑 – qbit 2014-09-26 09:42:06

+0

感謝排序的問題! – mark16 2014-09-26 09:43:49

0

您需要確保您的文件放置在您運行程序的同一目錄中。嘗試在主函數的頂部添加它,查看當前目錄是什麼,以及如果您的文件實際上位於該目錄中:

System.out.println(System.getProperty(「user.dir」));

+0

我發現文件路徑爲c:\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt,但是當我將File file = new File(「c:\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt「); c:\下劃線爲有錯誤:無效的轉義序列(有效的轉義序列是\ b \ t \ n \ f \ r \「\'\\ – mark16 2014-09-26 09:40:52

0
new File(String pathname); 

您正在使用的coinstructor將filepath作爲參數,絕對或相對。如果它是相對的,它將是執行路徑/ your_string。 所以你應該把文件放到編譯後的.jar文件所在的文件夾中。

File file1 = new File("text.txt"); 
File file2 = new File("D:/documents/test.txt"); 

如果PROGRAMM選自C執行:/programms/myprj.jar,所以 文件1將打開 「C:/programms/test.txt」 和file2將打開「d:/documents/test.txt 「獨立於執行路徑。

http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)

0

我張貼我的評論的答案,但我不允許發表評論,因爲我沒有足夠的聲譽。正如在你的評論中,你在文件路徑中使用反斜線。而是使用雙斜槓\或一個前進/。例如C:/java/file.txt 你應該爲它提供正確和實際的文件路徑,或者確保文件位於源代碼的位置。

public class ScannerReadFile { 

    public static void main(String[] args) { 

     // Location of file to read 
     File file = new File("C:/Users/EastCorporation/Desktop/CardNative.java.txt"); 

     try { 

      Scanner scanner = new Scanner(file); 

      while (scanner.hasNextLine()) { 
       String line = scanner.nextLine(); 
       System.out.println(line); 
      } 
      scanner.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

    } 
} 
+0

c:/現在下劃線爲有錯誤:轉義序列無效(有效的是\ b \ t \ n \ f \ r \「\'\\) – mark16 2014-09-26 09:41:51

1

這應該有效。

public static void main(String[] args) { 


      Scanner scanner = new Scanner(ScannerReadFile.class.getResourceAsStream("CardNative.java.txt")); 

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

    } 
}