2013-07-03 35 views
-2
public class NewClass1 { 

    public static void main(String[] args) throws FileNotFoundException { 

     String datasetFile = args[0]; 
     BufferedReader in = new BufferedReader(new FileReader(datasetFile)); 
} 
} 

它生成以下錯誤我想進入一個命令行參數爲我的字符串

Exception in thread "main" java.io.FileNotFoundException: abc (The system cannot find the file specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(FileInputStream.java:138) 
    at java.io.FileInputStream.<init>(FileInputStream.java:97) 
    at java.io.FileReader.<init>(FileReader.java:58) 
    at JavaApplication.NewClass1.main(NewClass1.java:29) 

我應該與此代碼代替它呢?

BufferedReader out = new BufferedReader(new 
         InputStreamReader(System.in)); 
String input = out.readLine(); 
+0

此鏈接可能會有幫助:http://users.cms.caltech.edu/~donnie/cs11/java/java-main.html –

+0

謝謝你的鏈接是有幫助的 –

+0

不客氣:) –

回答

1

這是一個命令行參數tutorial for netbeans。基本上,你去:

File-> Project Properties-> Run-> Arguments。

在你的代碼,你應該有:

try 
{ 
     if (args.length != 0) 
     { 
      datasetFile = args[0]; 
      in = new BufferedReader(new FileReader(datasetFile)); 
     } 
} 
catch(FileNotFoundException e) 
{ 
    e.printStackTrace(); 
} 

你的問題是問如何進入命令行參數,但它看起來像你這樣的方式,它的工作,因爲你在abc有一個FileNotFoundException ,所以這是你的問題。

+0

它仍然是產生錯誤。 –

+0

它說沒有找到該文件。這是你的錯誤。文件「abc」在同一目錄中嗎?如果不是,則需要傳遞其絕對路徑,即〜/ Users/Steve/Desktop/test.txt或C:\\ Users \\ Steve \\ Desktop \\ test.txt。 –

0

您剛剛收到的錯誤意味着文件'abc'不存在。你的代碼沒有問題。

相關問題