package cn.itcast.字节流四种方式复制文件;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 需求:把D:\\1.itcast复制到F盘目录下
*
* 字节流四种方式复制文件:文件大小:63.5 MB (66,601,516 字节)
* 基本字节流一次读写一个字节: 共耗时:
* 基本字节流一次读写一个字节数组: 共耗时:
* 高效字节流一次读写一个字节: 共耗时:
* 高效字节流一次读写一个字节数组: 共耗时:
*/
public class PandaDemo {
public static void main(String[] args) throws IOException{
long start = System.currentTimeMillis();
// method1("D:\\1.itcast","F:\\1.itcast");//基本字节流一次读写一个字节: 共耗时:640420毫秒
// method2("D:\\1.itcast","F:\\1.itcast");// 基本字节流一次读写一个字节数组: 共耗时:904毫秒
// method3("D:\\1.itcast","F:\\1.itcast");//高效字节流一次读写一个字节: 共耗时:6520毫秒
// method4("D:\\1.itcast","F:\\1.itcast");//高效字节流一次读写一个字节数组: 共耗时:292毫秒
//
long end = System.currentTimeMillis();
System.out.println("共用时:"+(end-start)+"毫秒");
}
private static void method4(String string, String string2)throws IOException {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(string));
BufferedOutputStream dos = new BufferedOutputStream(new FileOutputStream(string2));
byte [] by = new byte[1024];
int bys =0;
while((bys=fis.read(by))!=-1){
dos.write(by,0,bys);
}
fis.close();
dos.close();
}
private static void method3(String string, String string2)throws IOException {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(string));
BufferedOutputStream dos = new BufferedOutputStream(new FileOutputStream(string2));
int by=0;
while((by=fis.read())!=-1){
dos.write(by);
}
fis.close();
dos.close();
}
private static void method2(String string, String string2)throws IOException {
FileInputStream fis = new FileInputStream(string);
FileOutputStream dos = new FileOutputStream(string2);
byte [] by = new byte[1024];
int len=0;
while((len=fis.read(by))!=-1){
dos.write(by,0,len);
}
fis.close();
dos.close();
}
private static void method1(String string, String string2)throws IOException {
FileInputStream fis = new FileInputStream(string);
FileOutputStream dos = new FileOutputStream(string2);
int by = 0;
while((by=fis.read())!=-1){
dos.write(by);
}
fis.close();
dos.close();
}
}
|
|