java FileI(O)nputStream为什么比BufferedI(O)utputStream慢?
因?yàn)閎uffered多了一個(gè)緩沖區(qū),讀和寫都是先把硬盤或者內(nèi)存中的數(shù)據(jù)放到內(nèi)存中一塊緩存區(qū)域,到一定大小讀寫到硬盤或者內(nèi)存
?
package io;
import java.io.*;
public class FileIOTest {
??? /**
???? * @param args
???? * @throws FileNotFoundException
???? */
??? public static void main(String[] args) throws Exception {
??????? //有buff的File***Stream
??????? System.out.println("有buff的File***Stream耗時(shí):");
??????? new TimeTest() {
??????????? void run()throws Exception{
??????????????? FileInputStream fis = new FileInputStream("F:\\Test\\file.zip");
??????????????? FileOutputStream fos = new FileOutputStream("F:\\Test\\file1.zip");
??????????????? byte[] buf = new byte[1024];
??????????????? int length = 0;
??????????????? while ((length = fis.read(buf)) > 0) {
??????????????????? fos.write(buf, 0, length);
??????????????? }
??????????????? fis.close();
??????????????? fos.close();
??????????? }
??????? }.getTime();
???????
??????? //有buff的Buffered***Stream
??????? System.out.println("有buff的Buffered***Stream耗時(shí):");
??????? new TimeTest() {
??????????? void run()throws Exception{
??????????????? BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\Test\\file.zip"));
??????????????? BufferedOutputStream bos = new BufferedOutputStream(
??????????????????????? new FileOutputStream("F:\\Test\\file2.zip"));
???????????????
??????????????? byte[] buf = new byte[1024];
??????????????? int length = 0;
???????????????
??????????????? while ((length = bis.read(buf)) > 0) {
??????????????????? bos.write(buf, 0, length);
??????????????? }
??????????????? bis.close();
??????????????? bos.close();
???????????????
??????????? }
??????? }.getTime();
???????
??????? //無buff的File***Stream
??????? System.out.println("無buff的File***Stream耗時(shí):");
??????? new TimeTest() {
??????????? void run()throws Exception{
??????????????? FileInputStream fis = new FileInputStream("F:\\Test\\file.zip");
??????????????? FileOutputStream fos = new FileOutputStream("F:\\Test\\file3.zip");
??????????????? int data = 0;
??????????????? while ((data = fis.read()) !=-1) {
??????????????????? fos.write(data);
??????????????? }
??????????????? fis.close();
??????????????? fos.close();
??????????? }
??????? }.getTime();
???????
??????? //無buff的Buffered***Stream
??????? System.out.println("無buff的Buffered***Stream耗時(shí):");
??????? new TimeTest() {
??????????? void run()throws Exception{
??????????????? BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\Test\\file.zip"));
??????????????? BufferedOutputStream bos = new BufferedOutputStream(
??????????????????????? new FileOutputStream("F:\\Test\\file4.zip"));
???????????????
??????????????? int data = 0;
???????????????
??????????????? int i =bis.available();
???????????????
??????????????? while ((data = bis.read()) !=-1) {
??????????????????? bos.write((byte)data);
??????????????? }
??????????????? bis.close();
??????????????? bos.close();
??????????? }
??????? }.getTime();
??? }
}
//抽象的不太好的模板設(shè)計(jì)模式
abstract class TimeTest {
??? void getTime() {
??????? long start = System.currentTimeMillis();
??????? try {
??????????? run();
??????? } catch (Exception e) {
??????????? // TODO Auto-generated catch block
??????????? e.printStackTrace();
??????? }
??????? System.out.println(System.currentTimeMillis() - start);
??? }
??? abstract void run() throws Exception;
}
?
?
測試數(shù)據(jù)248kb
?
測試結(jié)果:
?
有buff的File***Stream耗時(shí):
8
有buff的Buffered***Stream耗時(shí):
2
無buff的File***Stream耗時(shí):
1369
無buff的Buffered***Stream耗時(shí):
14
轉(zhuǎn)載于:https://www.cnblogs.com/flying607/p/3460389.html
總結(jié)
以上是生活随笔為你收集整理的java FileI(O)nputStream为什么比BufferedI(O)utputStream慢?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: void和void*
- 下一篇: 牛刀小试、用SharePoint 实现请