cmdline 中的命令行参数与 IDE 中的命令行参数
如果我从实际命令行运行以下代码(即 javac ..., java XXX.java (args[0]) (args[1]),则以下代码的行为符合预期。
但是,如果我尝试通过设置命令行参数eclipse 我收到“输入或输出文件错误”错误,但如果 eclipse 中的 cmd 行参数长度!= 2 我还会收到“必须指定输入文件......”所以我知道它正在分配它们
有谁知道什么处理的是这个吗?
public class main {
public static Scanner fileScanner(String fName) throws FileNotFoundException {
return new Scanner(new FileInputStream(fName));
}
public static PrintStream printStream(String fName) throws FileNotFoundException {
return new PrintStream(new FileOutputStream(fName));
}
public static void main(String[] args) {
Scanner scan=null;
PrintStream out=null;
if(args.length != 2) {
System.out.println("Must specify input file & output file on cmd line");
System.exit(0);
}
try {
scan = fileScanner(args[0]);
out = printStream(args[1]);
} catch(FileNotFoundException e) {
System.out.println("Error with input or output file");
System.exit(0);
}
The following code behaves as expected if I run this from the actual command line (i.e. javac ..., java XXX.java (args[0]) (args[1]).
However if I try to set the command line args through eclipse I get the "Error with input or output file" error, but if the cmd line args in eclipse lenght != 2 I also get the "Must specify input file...." so I know it is assigning them
Does anyone know what the deal is with this?
public class main {
public static Scanner fileScanner(String fName) throws FileNotFoundException {
return new Scanner(new FileInputStream(fName));
}
public static PrintStream printStream(String fName) throws FileNotFoundException {
return new PrintStream(new FileOutputStream(fName));
}
public static void main(String[] args) {
Scanner scan=null;
PrintStream out=null;
if(args.length != 2) {
System.out.println("Must specify input file & output file on cmd line");
System.exit(0);
}
try {
scan = fileScanner(args[0]);
out = printStream(args[1]);
} catch(FileNotFoundException e) {
System.out.println("Error with input or output file");
System.exit(0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我试过你的程序,当我给出带有完整路径的文件名时,它在 Eclipse 中工作正常。
给出的参数: F:/temp/abc.txt F:/temp/xyz.txt
I tried you program, it works fine in eclipse when i give filename with complete path.
Args given: F:/temp/abc.txt F:/temp/xyz.txt