2012-10-12 35 views
0

我收到以下錯誤java.io.FileNotFoundException:in.txt,(該系統找不到指定的文件)

java.io.FileNotFoundException: in.txt, (The system cannot find the file specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileReader.<init>(Unknown Source) 
    at FileTest.main(FileTest.java:50) 

但林肯定的是,我已經下創建的文件in.txt src,bin和根目錄。我也嘗試在我的主要參數中指定完整的目錄,但仍然無法正常工作。爲什麼Eclipse沒有選擇它?

import java.io.*; 
public class FileTest { 
    public static void main(String[] args) { 

     try { 
      String inFileName = args[0]; 
      String outFileName = args[1]; 
      BufferedReader ins = new BufferedReader(new FileReader(inFileName)); 
      BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); 
      PrintWriter outs = new PrintWriter(new FileWriter(outFileName)); 

      String first = ins.readLine(); // read from file 
      while (first != null) { 
       System.out.print("Type in a word to follow " + first + ":"); 
       String second = con.readLine(); // read from console 
       // append and write 
       outs.println(first + ", " + second); 
       first = ins.readLine(); // read from file 
      } 
      ins.close(); 
      outs.close(); 
     } catch (IOException ex) { 
      ex.printStackTrace(System.err); 
      System.exit(1); 
     } 

    } 
} 
+0

您需要將其放在項目目錄IIRC的src目錄之外。 – nhahtdh

+0

@nhahtdh是的,我已經做到了,但仍然運氣... – user133466

+0

如何運行你的Java程序?我看到文件名是通過args參數接收的。 –

回答

2

我把你的代碼,用下面的命令行執行PARAMS它:

in.txt out.txt 

它與完全沒有問題。檢查你的命令行。

+0

所以params應該被一個空格分開,而不是逗號? – user133466

+0

是的,這是我們正在討論的命令行,就好像你是在命令提示符下輸入的一樣,唯一需要的分隔符是空格。 –

0

把它放在項目目錄,如果不工作,bin文件夾

0

打開「調試配置」和設置,標籤下的「參數」,工作目錄。相對路徑將相對於工作目錄。

6

鑑於錯誤消息,我猜測Java正在尋找名稱爲in.txt,的文件,並帶有尾隨逗號。

+0

我用逗號分隔我的第一個參數和我的第二個參數... – user133466

+0

你不應該使用空格分隔參數 –

+1

+1鷹眼...勾選[link](http://docs.oracle.com/javase/教程/必備/環境/ cmdLineArgs.html)。 – Gamb

2

默認情況下,Eclipse會將工作目錄設置爲項目文件夾。如果您已經更改了設置,你仍然可以通過這個代碼簡單的線條找出工作目錄:

System.out.println(new java.io.File("").getAbsolutePath()); 

把你的文本文件打印的文件夾中,並且你應該罰款。

0

我在我的eclipse中創建了一個新項目Learning,並將in.txt文件放在源目錄中。我使用示例java文件進行了測試。

public static void main(String[] args) { 
    File f = new File("a.txt"); 
    System.out.println(f.getAbsolutePath()); 
} 

輸出:

/home/bharathi/workspace/Learning/a.txt 

它看起來在一個項目的根目錄。您可以將in.txt放在項目的根目錄中,或者將路徑放到源目錄中。

0

可以有兩個問題:

  1. 在您的控制檯,與顯示的錯誤 '' 檢查 - >
    java.io.FileNotFoundException:in.txt,
    所以大概JVM尋找一個文件名','

  2. 第二種情況可以,你的in.txt文件不在 的位置。檢查它是否是項目的根。或主路徑 。

這將解決這些類型的問題。

相關問題