简单java复制

来源:百度知道 编辑:UC知道 时间:2024/05/31 23:33:47
下面程序怎么修改:
import java.io.*;
public class CopyFile {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream in=null;
FileOutputStream out=null;
int n=0;
try {
in=new FileInputStream(args[0]);
out=new FileOutputStream(args[1]);
while((n=in.read())!=-1){
out.write(n);
}
}catch(FileNotFoundException e){
System.out.println("找不到文件!");
}catch(IOException e){}
finally{System.out.println("复制成功!");
//in.close();
//out.close();
}
}
}
我用eclipse 运行的第一次运行 没反应 再点就出现下面错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at CopyFile.main(CopyFile.java:13)
目的:运行后 输入所要复制的文件
复制文件不要自己输入参数,也就是题中args[1],比如‘不懂

用eclipse 输入参数:
点击run - open run dialog
选择选项卡上的arguments 然后在program arguments 文本域中输入参数 如 :
d:\\1.txt
d:\\2.txt
一行为一个参数,然后点击apply,最后点击run

你的'复制成功'写在finally块中了。不管有没有异常都会执行。
不管有没有txt你都希望能复制成功。
那你就有file(String pathName)创建 file的实例,然后用createNewFile()(如果不存在就创建)创建txt,

对于用String[] args作为输入参数的程序,运行时需要注意:
1.在命令窗口运行:直接使用 java CopyFile args1,args2
2.使用eclipse运行: 需要预先设置参数,在运行-》参数-》运行参数里,填上参数(即args的具体值) 即可。否则直接运行的话,eclipse不会有系统默认参数,所以会报ArrayIndexOutOfBoundsException错误。

为什么要用字节流不用字符流呢··