您的位置:首页 > 职场人生

黑马程序员--IO流中使用字节流复制文件的效率比较

2015-08-07 23:14 513 查看
IO流中使用字节流复制文件有四种方法,分别是:

1.读写单个字节

2.读写字节数组

3.使用缓冲区的读写单个字节

4.使用缓冲区的读写字节数组

它们的效率究竟是怎么样,究竟相差多少呢,可以通过下列程序演示一下

其中复制的文件对象是一个20M左右的文件,时间单位是毫秒

1.读写单个字节的字节流复制文件

import java.io.*;

public class ByteCopy_FourType {
public static void main(String[] args) {
//获取程序开始时间
long start=System.currentTimeMillis();
copy_SingleByte();
//获取程序结束时间
long end=System.currentTimeMillis();
System.out.println(end-start);//打印程序执行时间结果为102234,也就是102秒多一点
}

/*
* 字节流读写单个字节
* */
public static void copy_SingleByte() {
//初始化输入输出流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//确定源文件和目标文件
fis=new FileInputStream("f:\\1.flv");//创建输入流对象
fos=new FileOutputStream("e:\\1.flv");//创建输出流对象
int len=0;
//挨个字节读取输入流数据
while((len=fis.read())!=-1){
//挨个字节写入
fos.write(len);
}
//处理异常
} catch (IOException ioe) {
ioe.printStackTrace();
//因为两个流都需要关闭,也就是都必须要执行,所以finally里要再写一个finally
} finally {
try{
fos.close();
}catch(Exception e){
throw new RuntimeException();
}finally{
try{
fis.close();
}catch(Exception e){
throw new RuntimeException();
}
}
}
}
}
2.读写字节数组

import java.io.*;

public class ByteCopy_FourType {
public static void main(String[] args) {
//获取程序开始时间
long start=System.currentTimeMillis();
copy_ByteArray();
//获取程序结束时间
long end=System.currentTimeMillis();
System.out.println(end-start);//打印程序执行时间结果为162,也就是0.16秒多一点
}
/*
* 字节流读写字节数组
* */
public static void copy_ByteArray(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//确定源目录和目标目录
fis=new FileInputStream("f:\\1.flv");
fos=new FileOutputStream("e:\\1.flv");
byte[] bytes =new byte[1024];
int len=0;
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try{
fos.close();
}catch(Exception e){
throw new RuntimeException();
}finally{
try{
fis.close();
}catch(Exception e){
throw new RuntimeException();
}
}
}
}
}
3.使用缓冲区的读写单个字节

public class ByteCopy_FourType {
public static void main(String[] args) {
//获取程序开始时间
long start=System.currentTimeMillis();
copy_ByteArray();
//获取程序结束时间
long end=System.currentTimeMillis();
System.out.println(end-start);//打印程序执行时间结果为1516,也就是1.5秒多一点
}
/*
* 字节流缓冲区读写单个字节
* */
public static void copy_SingleByteB(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//确定源目录和目标目录
bis=new BufferedInputStream(new FileInputStream("f:\\1.flv"));
bos=new BufferedOutputStream(new FileOutputStream("e:\\1.flv"));
int len=0;
while((len=bis.read())!=-1){
bos.write(len);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try{
bos.close();
}catch(Exception e){
throw new RuntimeException();
}finally{
try{
bis.close();
}catch(Exception e){
throw new RuntimeException();
}
}
}
}
}


4.使用缓冲区的读写字节数组

import java.io.*;

public class ByteCopy_FourType {
public static void main(String[] args) {
//获取程序开始时间
long start=System.currentTimeMillis();
copy_ByteArray();
//获取程序结束时间
long end=System.currentTimeMillis();
System.out.println(end-start);//打印程序执行时间结果为54,也就是0.054秒多一点
}
/*
* 字节流缓冲区读写字节数组
* */
public static void copy_ByteArrayB(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//确定源目录和目标目录
bis=new BufferedInputStream(new FileInputStream("f:\\1.flv"));
bos=new BufferedOutputStream(new FileOutputStream("e:\\1.flv"));
byte[] bytes =new byte[1024];
int len=0;
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try{
bos.close();
}catch(Exception e){
throw new RuntimeException();
}finally{
try{
bis.close();
}catch(Exception e){
throw new RuntimeException();
}
}
}
}
}


他们各自所需的时间是:

第一种:

102234毫秒,也就是102秒多一点
第二种

162毫秒,也就是0.16秒多一点,效率提高了600多倍
第三种

1516毫秒,也就是1.5秒多一点,效率相对于第一种来说提高了将近70倍
第四种

54毫秒,也就是0.054秒多一点,效率整整提高了将近1900倍!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: