java文件流有几种_JAVA中几种文件操作流的效率对比

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

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 hm = new TreeMap(new Comparator(){

@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 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值