有人发帖问下面的程序为什么运行会出错?
The method cypher(InputStream, OutputStream) in the type
MyEncoderText is not applicable for the arguments (FileInputStream,
FileOutputStream)
package package1;
import java.io.*;
public class MyEncoderText {
public static void main(String[] args) throws Exception {
String path0 = args[0];
String path1 = args[1];
FileInputStream in = new FileInputStream(path0);
FileOutputStream out = new FileOutputStream(path1);
cypher(in, out);
System.out.println("结束");
out.close();
}
private static void cypher(InputStream inp, OutputStream oup)
throws Exception {
int len = -1;
System.out.println("0: " + len);
while ((len = inp.read()) != -1) {
System.out.println("1: " + len);
oup.write(len);// ^ 102 ^ 91
System.out.println("2: " + len);
}
System.out.println("3: " + oup);
}
}
然后,我试了一下,好像没有问题啊?
有人建议他把cypher方法的参数改为(FileInputStream inp, FileOutputStream oup),
就这样结束了这个提问。无聊。
还有那个oup.write(len^ 102 ^ 91);
我开始以为是类似二进制移位这样提高效率之类的操作,
结果根本不行!把它注释掉才能真正地执行复制文件的操作,浪费我时间。
不过这个程序倒是提醒了我关于“在运行的时候传递参数”的问题,
写了一个小程序:
public class TestArgs {
public static void main(String[] args) {
String Arg1 = args[0];
String Arg2 = args[1];
String Arg3 = args[2];
System.out.println(Arg1);
System.out.println(Arg2);
System.out.println(Arg3);
}
}
在cmd小黑窗或者Eclipse中,只要以空格隔开传递的参数,
就可以将参数传递给程序。
例如:java TestArgs A B C 这样。
Eclipse就是Run configurations……窗口传,不截图了。
转载于:https://blog.51cto.com/sesedese/1585705