思考:有了inputStream为什么还需要BufferedInputStream?
inputStream和bufferedInputStream的区别就是后者带有缓冲区,前者没有
inoutStream在读取的时候,每读一个字节就需要写入一个字节,这样就会频繁的进行IO操作
bufferedInputStream带缓冲区的流,可以一次性读取很多字节,但是不往磁盘中写入,只是放在内存中,等凑够了缓冲区大小的是时候,一次性写入到磁盘中
也就引生出了一个需要注意的地方
在使用bufferedInputStream读写完数据之后,记得需要使用flush()方法或者close()方法,强制的将缓冲区中的数据写出
flush和close的区别
close 在close时候,会自动进行flush
在不调用close,并且缓冲区未满的情况下,又需要把缓冲区的内容写入到文件,这时候用flush
写代码进行对比两个时间消耗
import java.io.*;
import java.util.zip.CRC32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author luodong
* @date 2021/8/11
*/
public class ZipResult {
//主函数
public static void main(String[] args) {
copyFile();
copyFileInputStream();
}
public static void copyFile() {
long startTime = System.currentTimeMillis();
String filePath="/Users/luodong/Documents/photo/algorithm-base-main.zip";
String copyPath = "/Users/luodong/Documents/test/algorithm-base-main.zip";
FileInputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
FileOutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
inputStream = new FileInputStream(filePath);
bufferedInputStream = new BufferedInputStream(inputStream);
outputStream = new FileOutputStream(copyPath);
bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] b = new byte[1024];
int length = 0;
while((length=bufferedInputStream.read(b))!=-1){
bufferedOutputStream.write(b, 0, length );
}
bufferedOutputStream.flush();
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime+"耗时");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if( bufferedOutputStream != null ){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if( bufferedInputStream != null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if( inputStream != null ){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( outputStream != null ) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void copyFileInputStream() {
long startTime = System.currentTimeMillis();
String filePath="/Users/luodong/Documents/photo/algorithm-base-main.zip";
String copyPath = "/Users/luodong/Documents/test/algorithm-base-main.zip";
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream(filePath);
outputStream = new FileOutputStream(copyPath);
byte[] b = new byte[1024];
int length = 0;
while((length=inputStream.read(b))!=-1){
outputStream.write(b, 0, length );
}
outputStream.flush();
long endTime = System.currentTimeMillis();
System.out.println(endTime-startTime+"耗时");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if( inputStream != null ){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( outputStream != null ) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
运行结果
xxx.utils.ZipResult
1耗时
2耗时
Process finished with exit code 0