2014-03-12 83 views
-1

這是我的代碼片段:BufferedReader中找不到文件

static String filepath = "test.txt"; 
    public static void main(String[] args) throws IOException { 
     try{ 
     BufferedReader reader = new BufferedReader(new FileReader(filepath)); 
     String l = reader.readLine(); 
     System.out.println(l); 
     }catch (FileNotFoundException e){ 
      System.out.println(e); 
     } 
     catch(IOException e){ 
      System.out.println(e); 
     } 

java.io.FileNotFoundException: test.txt (No such file or directory) 這是我得到的例外。

我應該使用什麼文件路徑?我把文本文件放在bin文件夾中。

我在Ubuntu上運行Eclipse,它很重要。

+0

嘗試把文件放在你運行'java'的地方,bin在你的類路徑中,閱讀它你也可以使用Class.getResourceAsStream() – Safrain

+0

你應該必須在FileReader中包含文件路徑。文件路徑不是文件的名稱。 – user1658435

回答

0

如果您正在運行eclipse並擁有一個java項目,那麼只需將該文件放入項目中即可使用。

0

將文件放在項目文件夾的根目錄下。如果你的項目根目錄是FileReader,那麼將文件放入它