我想为我的字符串输入一个命令行参数

时间:2013-07-03 06:53:53

标签: java netbeans-7

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(); 

2 个答案:

答案 0 :(得分:1)

这是一个命令行参数tutorial for netbeans。基本上,你去:

  

文件 - &gt;项目属性 - &gt;运行 - &gt;参数。

在您的代码中,您可能应该:

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

你的问题询问如何输入命令行参数,但它看起来像你工作的方式,因为FileNotFoundException上有abc,这就是你的问题。

答案 1 :(得分:0)

您获得的错误仅表示文件'abc'不存在。您的代码没有任何问题。