流-复制文件

所谓的复制文件:也就是从一个文件里面读,然后在写入到另外一个文件当中.
一些代码如下:

/**
*

  • @author dch

*/
//复制文件 先读后写
public class TestCopy {

public static void main(String[] args) throws Exception {

	
	//copy1();
	//copy2();

}

public  static void copy1() throws Exception{
	
	//先从一个文件中读出
	InputStream is = new FileInputStream("d:\\abc/aaa.txt");
	
	//再写入另一个文件中
	OutputStream os = new FileOutputStream("d:\\adc/aaa.txt");
	
	long start =System.currentTimeMillis();
	int s = is.read();
	while(s != -1){
		os.write(s);
		s=is.read();	
		System.out.println(s);
	}
	
	long end = System.currentTimeMillis();
	System.out.println("文件复制完成,用了: "+(end-start));
	os.close();
	is.close();
}

public static void copy2() throws Exception{
	
	InputStream is = new FileInputStream("d://测试文件夹/a/a.txt");
	OutputStream os = new FileOutputStream("d://测试文件夹/b/b.txt");
	
	byte [] b = new byte[1024];
	int len = 0;
	long start = System.currentTimeMillis();
	while((len=is.read(b))!=-1){
		//写入时按照实际读取的长度
		os.write(b, 0, len);
	}
	long end = System.currentTimeMillis();
	System.out.println("复制花费时间: "+(end-start));
	os.close();
	is.close();
}

}

猜你喜欢

转载自blog.csdn.net/qq_41035395/article/details/88912926