实现文件拷贝的共轭能

来源:互联网 发布:郭德纲说网络 编辑:程序博客网 时间:2024/05/07 17:24
package iotest.javase.test;
import java.io.*;
public class testiocopye_04 {
public static void main(String[] args) throws IOException {
//如果要执行拷贝命令,则必须通过初始化参数传递源文件路径及目标文件路径
if (args.length !=2) {//参数内容必须是两个
System.out.println("错误的命令,请输入正确的命令");
System.exit(1);//退出程序
}
//如果现在有参数了。那么还要验证源文件是否存在
File infile = new File(args[0]);
if (!infile.exists()) {
System.out.println("路径错误,请输入源文件的正确路径。");
System.exit(1);
}
//如果拷贝的目标文件存在,则也不应该进行拷贝
File outfile=new File(args[1]);
if (outfile.exists()) {//目标文件已经存在
System.out.println("拷贝的路径已经窜在,请更换路径。");
System.exit(1);
}
long start = System.currentTimeMillis();
InputStream in= new FileInputStream(infile);
OutputStream out= new FileOutputStream(outfile);
copy(in,out);//实现文件拷贝
in.close();
out.close();
long end=System.currentTimeMillis();
System.out.println("拷贝的时间"+(end-start));//文件拷贝所需要用的时间
}


private static void copy(InputStream in, OutputStream out) throws IOException {
int temp = 0;//保存每次读取的字节量
while((temp = in.read()) !=-1){
out.write(temp);
}
}
}