前面介绍了基本的字节流和字符流,那只是基本,如果要处理一个很大的文件,那就要在节点流前加入BufferedOutputStream 和BufferedInputStream 这两个处理流,这样能大大提高我们的效率.
package com.buffer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.ReadableByteChannel;
/**
*
* @author Yijun
* 这次我们来看下buffer的作用,用了readByChannel,readNotbuffer,readBuffer三种方法比较buffer读的优异性
* 用writeBuffer,writeNoBuffer比较buffer写的优异性
*/
public class TestBufferFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String src = "F:\\java_test\\a.jpg";// 原文件
String src2 = "F:\\java_test\\copy\\a.jpg";// 目标文件1
String src3 = "F:\\java_test\\copy\\aCopy.jpg";// 目标文件2
long st = System.currentTimeMillis();// 获得现在时间点
readByChannel(src);// ReadableByteChannel 方法
System.out.println("channel : " + (System.currentTimeMillis() - st));
st = System.currentTimeMillis();
readNotbuffer(src);// 不用buffer读取文件
System.out.println("buffered : " + (System.currentTimeMillis() - st));
st = System.currentTimeMillis();
readBuffer(src);// 使用buffer读取文件
System.out.println("not buffered : "
+ (System.currentTimeMillis() - st));
/***************************************************************/
st = System.currentTimeMillis();
writeBuffer(src2, src);// 使用buffer写文件
System.out.println("writeBuffer : "
+ (System.currentTimeMillis() - st));
/*********************************************************************/
st = System.currentTimeMillis();
writeNoBuffer(src3, src);// 不使用buffer写文件
System.out.println("writeNoBuffer : "
+ (System.currentTimeMillis() - st));
}
private static void writeNoBuffer(String src2, String src)
throws IOException {
FileInputStream fin = new FileInputStream(src);
FileOutputStream fout = new FileOutputStream(src2);
byte[] array = new byte[1024];
int len = 0;
while ((len = fin.read(array)) > 0) {
fout.write(array, 0, len);
}
}
private static void writeBuffer(String src2, String src) throws IOException {
FileOutputStream fout = new FileOutputStream(src2);
BufferedOutputStream bout = new BufferedOutputStream(fout);
FileInputStream fin = new FileInputStream(src);
BufferedInputStream bin = new BufferedInputStream(fin);
byte[] data = new byte[1024];
while (bin.read(data) != -1) {
bout.write(data);
}
bout.flush();// //将缓冲区中的数据全部写出
}
public static void readNotbuffer(String src) throws IOException {
FileInputStream fin = new FileInputStream(src);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fin.read(buffer)) > 0) {
}
fin.close();
}
public static void readBuffer(String src) throws IOException {
FileInputStream fin = new FileInputStream(src);
BufferedInputStream bufferin = new BufferedInputStream(fin, 1024);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bufferin.read(buffer)) > 0) {
}
bufferin.close();
fin.close();
}
public static void readByChannel(String src) throws IOException {
FileInputStream fin = new FileInputStream(src);
ReadableByteChannel channel = fin.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);// 创建一个指定1024的ByteBuffer
while (channel.read(buffer) > 0) {
buffer.flip();// 读写状态切换
buffer.clear();// 清空
}
fin.close();
}
}
我们运行代码后就能发现,使用buffer的好处,提高了我们处理的时间..