import java.io.*;
public class Test1 {
public static void main(String[] args) throws IOException {
File src=new File("C:\\Users\\Public\\Videos\\Sample Videos\\1.wmv");//25M视频
File dest1=new File("C:\\Users\\Public\\Videos\\Sample Videos\\2.wmv");
File dest2=new File("C:\\Users\\Public\\Videos\\Sample Videos\\3.wmv");
File dest3=new File("C:\\Users\\Public\\Videos\\Sample Videos\\4.wmv");
File dest4=new File("C:\\Users\\Public\\Videos\\Sample Videos\\5.wmv");
Long start1=System.currentTimeMillis();
copyByByte(src,dest1);
Long end1=System.currentTimeMillis();
System.out.println(end1-start1);//209091毫秒
Long start2=System.currentTimeMillis();
copyByBytes(src,dest2);
Long end2=System.currentTimeMillis();
System.out.println(end2-start2);//84毫秒
Long start3=System.currentTimeMillis();
copyByBuffered(src,dest3);
Long end3=System.currentTimeMillis();
System.out.println(end3-start3);//376毫秒
Long start4=System.currentTimeMillis();
copyByBufferedBytes(src,dest4);
Long end4=System.currentTimeMillis();
System.out.println(end4-start4);//71毫秒
}
//一个字节一个字节复制
public static void copyByByte(File src,File dest) throws IOException{
FileOutputStream fos=new FileOutputStream(dest);
FileInputStream fis=new FileInputStream(src);
int b;
while((b=fis.read())!=-1){
fos.write(b);
}
fos.flush();
fis.close();
fos.close();
}
//批量复制
public static void copyByBytes(File src,File dest) throws IOException{
FileOutputStream fos=new FileOutputStream(dest);
FileInputStream fis=new FileInputStream(src);
byte[] bytes=new byte[10240];
int b;
while((b=fis.read(bytes, 0, bytes.length))!=-1){
fos.write(bytes, 0, b);
}
fos.flush();
fis.close();
fos.close();
}
//缓冲复制
public static void copyByBuffered(File src,File dest) throws IOException{
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
int b;
while((b=bis.read())!=-1){
bos.write(b);
}
bos.flush();
bis.close();
bos.close();
}
//缓冲+批量复制
public static void copyByBufferedBytes(File src,File dest) throws IOException{
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(dest));
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
byte[] bytes=new byte[10240];
int b;
while((b=bis.read(bytes, 0, bytes.length))!=-1){
bos.write(bytes, 0, b);
}
bos.flush();
bis.close();
bos.close();
}
}
點擊查看更多內(nèi)容
1人點贊
評論
評論
共同學(xué)習(xí),寫下你的評論
評論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦