高效流和基本流、字节和字节数组读取一个视频文件速度的比较程序

package org.westos_02;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.FileInputStream;

import java.io.FileOutputStream;

存储文件

 * IO流:永久存储(耗时)
 * 数据库:永久存储
 * 
 * 基本的字节流
 * 文件字节输入流/文件字节输出流
 * 高效的字节流(缓冲流)
 * 
 * 操作一个视频文件,来测试速度问题
 * 基本的字节流一次读取一个字节 ://耗时:85772毫秒
 * 基本的字节流一次读取一个字节数组 :共耗时:216毫秒
 * 
 * 高效的字节流一次读取一个字节      :共耗时:682毫秒
 * 高效的字节流一次读取一个字节数组:共耗时:49毫秒
 * 
 * @author Administrator
 *
 *
 *StringBuffer:提供了一个字符串缓冲区 (可以在缓冲区中不断追加字符串)
 */
public class Test {

public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis() ;

// method1("e:\\abc.mp4","copy1.mp4") ;
// method2("e:\\abc.mp4","copy2.mp4") ;
// method3("e:\\abc.mp4","copy3.mp4") ;
method4("e:\\abc.mp4","copy4.mp4") ;

long end = System.currentTimeMillis() ;

System.out.println("共耗时:"+(end-start)+"毫秒");
}


// method1基本的字节流一次读取一个字节
private static void method1(String src, String dest) throws Exception {
//封装源文件和目标文件
FileInputStream fis = new FileInputStream(src) ;
FileOutputStream fos = new FileOutputStream(dest) ;

//读写操作
int by = 0 ;
while((by=fis.read())!=-1) {
//写
fos.write(by);
}

//释放资源
fis.close();
fos.close();

}

// method2基本的字节流一次读取一个字节数组
private static void method2(String src, String dest) throws Exception {
//封装文件
FileInputStream fis = new FileInputStream(src) ;
FileOutputStream fos = new FileOutputStream(dest) ;

//读写操作
byte[] bys = new byte[1024] ;//相当于一个缓冲区
int len = 0 ;
while((len=fis.read(bys))!=-1) {
fos.write(bys, 0, len);
}

//释放资源
fis.close();
fos.close();

}


//method3高效的字节流一次读取一个字节
private static void method3(String src, String dest) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src))  ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)) ;

//一次读个字节
int by = 0 ;
while((by=bis.read())!=-1) {
bos.write(by);
}
//释放资源
bis.close();
bos.close();

}

//method4高效的流一次读取一个字节数组
private static void method4(String src, String dest) throws Exception {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src))  ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest)) ;

//一次读取一个字节数组
byte[] bys = new byte[1024] ; 
int len = 0 ;
while((len=bis.read(bys))!=-1) {
bos.write(bys, 0, len);
}

bis.close();
bos.close();

}}

猜你喜欢

转载自blog.csdn.net/qq_41141896/article/details/80411804