如果我从实际命令行(即javac ...,java XXX.java(args [0])(args [1])运行此代码,则以下代码的行为与预期一致。
但是,如果我尝试通过eclipse设置命令行args,我会得到“输入或输出文件错误”错误,但如果cmd行args在eclipse lenght!= 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);
}
答案 0 :(得分:0)
我试过你的程序,当我给出完整路径的文件名时,它在eclipse中工作正常。
package so.ch1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class main {
/**
* @param args
*/
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);
}
}
}
Args给出:F:/temp/abc.txt F:/temp/xyz.txt