JAVA中几种文件操作流的效率对比

来源:互联网 发布:log4j2.xml sql日志 编辑:程序博客网 时间:2024/06/06 01:38

以下案例说明了分别采用字节流方式、字节缓冲流方式读写文件的效率对比

package ioxcercise;


import Java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Comparator;

import java.util.Set;

import java.util.TreeMap;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

 

/*

 * 需求:把其他目录下的视频文件复制到指定目录中

 * 视频文件大小:49MB

 * 要求使用字节流的四种方式复制文件,比较四种方式的耗时:

 * 基本字节流一次读写一个字节

 * 基本字节流一次读写一个字节数组

 * 高效字节流一次读写一个字节

 * 高效字节流一次读写一个字节数组

 */

 

public class ioRWcomparation {

 

public staticvoid main(String[] args)throws IOException {

String from = "D:\\1oldBoy_4\\1216\\day12\\videos_day12\\testvideo.avi";

String to = "D:\\1oldBoy_4\\1216\\day18\\testvideo.rmvb";

TreeMap<Integer,String> hm = new TreeMap<Integer,String>(new Comparator<Integer>(){

@Override

public int compare(Integer int1, Integer int2) {

int num = int1 - int2;

int num2 = num == 0 ? int1.compareTo(int2) : num;

return num2;

}

});

//字节流:每次写入单个字节的耗时

long long1 = SimpleIoStream(from, to);

hm.put((int) long1,"字节流:每次写入单个字节的耗时(毫秒)");

 

//字节流:每次写入指定长度的字节数组的耗时

long long2 = MultiWritingStream(from, to);

hm.put((int) long2,"字节流:每次写入指定长度的字节数组的耗时(毫秒)");

//字节缓冲流:每次写入单个字节的耗时

long long3 = highEfficientSimpleStream(from, to);

hm.put((int)long3,"字节缓冲流:每次写入单个字节的耗时(毫秒)");

//字节缓冲流:每次写入指定长度的字节数组的耗时

long long4 = highEfficientMultiStream(from, to);

hm.put((int)long4,"字节缓冲流:每次写入指定长度的字节数组的耗时(毫秒)");

Set<Integer> set = hm.keySet();

for(Integer i : set){

System.out.println(hm.get(i)+" : "+i);

}

}

 

private staticlong highEfficientMultiStream(String from, String to)throws IOException{

long start = System.currentTimeMillis();

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(from));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(to));

byte[] readBytes =newbyte[1024];

int readResult = 0;

while((readResult = bis.read(readBytes)) != -1){

bos.write(readBytes, 0, readResult); //高效字节流一次读写一个字节数组

}

return System.currentTimeMillis() - start;

}

 

private staticlong highEfficientSimpleStream(String from, String to)throws IOException{

long start = System.currentTimeMillis();

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(from));

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(to));

int len = 0;

while((len=bis.read()) != -1){//高效字节流一次读写一个字节

bos.write(len);

}

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

return System.currentTimeMillis() - start;

}

 

private static long MultiWritingStream(String from, String to)throws IOException {

long start = System.currentTimeMillis();

FileInputStream fis = new FileInputStream(from);

FileOutputStream fos = new FileOutputStream(to);

int len = 0;

byte[] bArr =newbyte[2048];

while((len=fis.read(bArr)) != -1){//基本字节流:一次读写一个字节数组

fos.write(bArr, 0, len);

}

fis.close(); fos.close();

return System.currentTimeMillis() - start;

}

 

public static long SimpleIoStream(String from, String to)throws IOException {

long start = System.currentTimeMillis();

FileInputStream fis = new FileInputStream(from);

FileOutputStream fos = new FileOutputStream(to);

int len = 0;

while ((len = fis.read()) != -1) {//基本字节流:一次读写一个字节

fos.write(len);

}

fis.close(); fos.close();

return System.currentTimeMillis() - start;

}

}


 

【使用大小为49MB的视频读写结果】

字节缓冲流:每次写入指定长度的字节数组的耗时(毫秒) : 212

字节流:每次写入指定长度的字节数组的耗时(毫秒) : 527

字节缓冲流:每次写入单个字节的耗时(毫秒) : 1134

字节流:每次写入单个字节的耗时(毫秒) : 710350


可以看出,缓冲流方式的读写速率明显比非缓冲流要大,每次读写字节数组的速率明显大于每次读写单个字节的速率.


来源 :http://blog.csdn.net/osjedufei