/*
* 复制视频
* 4种方式 共耗时
* 基本的字节流,一次一个字节 共耗时:229657
* 基本的字节流,一次一个字节数组 共耗时: 431
* 高效的字节流,一次一个字节 共耗时:1723
* 高效的字节流,一次一个字节数组 共耗时:31
*/
public class Demo {
public static void main(String[] args) throws IOException {
long start =System.currentTimeMillis();
method1("test.avi","copyAVi1.avi");
method2("test.avi","copyAVi1.avi");
method3("test.avi","copyAVi1.avi");
method4("test.avi","copyAVi1.avi");
long end=System.currentTimeMillis();
System.out.println("共耗时:" +(end-start));
}
//高效的字节流,一次一个字节数组
public static void method4(String str, String des) throws IOException {
BufferedInputStream bos =new BufferedInputStream(new FileInputStream(str));
BufferedOutputStream bis =new BufferedOutputStream(new FileOutputStream(des));
byte[] buffer=new byte[1024];
int len=-1;
while((len=bos.read(buffer))!=-1){
bis.write(len);
}
bos.close();
bis.close();
}
//高效的字节流,一次一个字节
private static void method3(String str, String des) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(str));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(des));
int ch=-1;
while((ch=bis.read())!=-1){
bos.write(ch);
}
bis.close();
bos.close();
}
//基本的字节流,一次一个字节数组
public static void method2(String str, String des) throws IOException {
//数据源
FileInputStream fis=new FileInputStream(str);
//目的地
FileOutputStream fos=new FileOutputStream(des);
byte[]buffer =new byte[1024];
//读数据
int len=-1;
while((len=fis.read(buffer))!=-1){
//写数据
fos.write(len);
}
//关闭
fis.close();
fos.close();
}
//基本的字节流,一次一个字节
public static void method1(String str, String des) throws IOException {
//数据源
FileInputStream fis=new FileInputStream(str);
//目的地
FileOutputStream fos=new FileOutputStream(des);
//读数据
int ch=-1;
while((ch=fis.read())!=-1){
//写数据
fos.write(ch);
}
//关闭
fis.close();
fos.close();
}
} |
|