`
benx
  • 浏览: 273323 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

FileInputStream和BufferedInputStream

    博客分类:
  • java
J# 
阅读更多

 

万恶的javaeye,编辑器能不能优化下,每次修改样式都变形了。

 

FileInputStream 属于数据源

BufferedInputStream 属于FileInputStream的一个装饰

 

 

BufferedInputStream 有个内部缓冲区当read时会先把缓冲区填满,然后下次读取是直接从缓冲区读取。当读取的位置大于缓冲区时会再一次加载缓冲区。

 

read()read(byte[] buf, int off, int len)处理方式一样,区别在于后者一次返回多个数据,但是同样都是先放入缓冲区,然后再读取。

private void fill() throws IOException {

       byte abyte0[] = getBufIfOpen();

       if (markpos < 0)

           pos = 0;

       else if (pos >= abyte0.length)

           if (markpos > 0) {

              int i = pos - markpos;

              System.arraycopy(abyte0, markpos, abyte0, 0, i);

              pos = i;

              markpos = 0;

           } else if (abyte0.length >= marklimit) {

              markpos = -1;

              pos = 0;

           } else {

              int j = pos * 2;

              if (j > marklimit)

                  j = marklimit;

              byte abyte1[] = new byte[j];

              System.arraycopy(abyte0, 0, abyte1, 0, pos);

              abyte0 = abyte1;

           }

       count = pos;

       int k = getInIfOpen().read(abyte0, pos, abyte0.length - pos);

       if (k > 0)

           count = k + pos;

    }

 

    public synchronized int read() throws IOException {

       if (pos >= count) {

           fill();

           if (pos >= count)

              return -1;

       }

       return getBufIfOpen()[pos++] & 255;

    }

 

至于性能问题,我们都知道文件的物理读取性能肯定要大于内存读取,FileInputStream.read()相当于一次物理读取,而BufferedInputStream .read()大部分相当于一次内存读取。

 

 

 

同理FileOutputStreamBufferedOutputStream原理也是一样,也是有个缓冲区,每次write相当于写入缓冲区,当缓冲区慢了后在物理写入文件。

private void flushBuffer()

        throws IOException

    {

        if(count > 0)

        {

            out.write(buf, 0, count);

            count = 0;

        }

    }

 

    public synchronized void write(int i)

        throws IOException

    {

        if(count >= buf.length)

            flushBuffer();

        buf[count++] = (byte)i;

    }

 

 

 

测试

 

public static void bufferTest() throws IOException{

       BufferedInputStream is = new BufferedInputStream(new FileInputStream("d:/A.exe"));

       BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/B.exe"));

       byte[] buffer = new byte[100];

      

       System.out.println("buffer start "+new Date());

       int size = 0 ;

       while((size=is.read(buffer, 0, buffer.length))!=-1){

           out.write(buffer);

       }

       is.close();

       out.close();

       System.out.println("buffer end "+new Date());

    }

   

    public static void ioTest()throws IOException{

       FileInputStream is = new FileInputStream("d:/A.exe");

       FileOutputStream out = new FileOutputStream("d:/C.exe");

       byte[] buffer = new byte[100];

      

       System.out.println("io start "+new Date());

       int size = 0 ;

       while((size=is.read(buffer, 0, buffer.length))!=-1){

           out.write(buffer);

       }

       is.close();

       out.close();

       System.out.println("io end "+new Date());

    }

 

文件大概有1G,用buffer大概用了20秒,用原始方式等了1分钟还没完,直接强制终止。

分享到:
评论
2 楼 benx 2011-12-31  
xuhang1128 写道
可是如果把ioTest方法里面的缓冲区调大,比如和BufferedInputStream里面默认的缓冲区一样大, byte[] buffer = new byte[1024*8];好像速度也差不多啊?咋回事啊


BufferedInputStream的默认缓冲区是8192(4K),说明每次物理读取4K,然后FileInputStream的每次read,如果缓冲区有就从缓冲区读,没有就开始物理读,fill buffer

如果FileInputStream 的read(byte[] buffer) 的buffer大小为8192相当于每次物理读取8192,所以速度和BufferInputStream一致的,因为两个的物理读次数一致。

他们的不同点就是BufferedInputStream如果需要物理读,那么默认一次读取N个数据到buffer,然后以后的每次read(byte b)都是先从buffer读取,buffer没有了就开始物理读。
1 楼 xuhang1128 2011-12-28  
可是如果把ioTest方法里面的缓冲区调大,比如和BufferedInputStream里面默认的缓冲区一样大, byte[] buffer = new byte[1024*8];好像速度也差不多啊?咋回事啊

相关推荐

    java字符流练习.doc

    在这个示例中,我们使用了 FileInputStream 和 BufferedInputStream 读取文件中的内容,并使用循环来统计字母 'A' 和 'a' 的出现次数。 示例 3: 统计文件中的各个字母出现次数 在第三个示例中,我们将演示如何使用...

    bytestreamdemo.zip

    FileInputStream和FileOutputStream BufferedInputStream 和 BufferedOutputStream DataInputStream 和 DataOutputStream ObjectInputStream和ObjectOutputStream PrintStream PushbackInputStream

    jlayer-1.0.1.jar

    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //创建播放器对象,把文件的缓冲流传入进去 player = new Player(bufferedInputStream); ...

    java中实现复制文件和文件夹

    java中实现复制文件和文件夹 public static void copyFile(File sourceFile,File targetFile) throws IOException{ // 新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile)...

    使用J2SE API读取Properties文件的六种方法

    InputStream in = lnew BufferedInputStream(new FileInputStream(name));  Properties p = new Properties();  p.load(in);  2。使用java.util.ResourceBundle类的getBundle()方法  示例: ResourceBundle rb ...

    java IO章节的总结

    IO从大的方向上分为字节流和字符流,包括四个抽象类: 1、输入:Reader, InputStream类型的子类(字符,字节) 2、输出:Writer, OutputStream类型的子类(字符,字节) 决定使用哪个类以及它的构造进程的一般...

    用字节流实现readline功能

    使用BufferedInputStream字节流,以及StringBuiler技术实现字节的逐行读取。即字节流的readline功能。BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));

    java播放MP3

    BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(music)); player = new Player(buffer); player.play(); } public static void main(String[] args) throws JavaLayerException {...

    java压缩文件夹为zip

    java.util.zip工具包里面的类实现文件夹的zip压缩。具体会用到FileInputStream,CheckedOutputStream,BufferedInputStream,ZipOutputStream.

    java高级特性 - io流.docx

    常见的字节流类包括:FileInputStream、FileOutputStream 用于文件操作;ByteArrayInputStream、ByteArrayOutputStream 用于字节数组操作;BufferedInputStream、BufferedOutputStream 用于缓冲操作等。 字符流...

    文件操作,文件写入 读文件数据

    文件的创建,文件夹的创建,分别以FileOutputStream、FileInputStream、BufferedOutputStream、BufferedInputStream、FileWriter、FileReader、BufferedWriter、BufferedReader读取文件数据,向文件写入数据。

    JavaIo流分析图

    分析Java常用IO流,包括InputStream、OutputStream、FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream、Reader、Writer、InputStreamReader、OutputStreamWriter、FileReader、...

    JAVA IO流缓冲字节流缓冲字符流等流经典代码示例加注释总结.rar

    FileWriter、FileReader、CharArrayReader、CharArrayWriter、CharSequence、OutputStreamWriter、FileOutputStream、InputStreamReader、PrintWriter、BufferedReader、InputStream、FileInputStream、OutputStream...

    IO流各个类的使用方法

    FileInputStream FileOutputStream DataInputStream DataOutputStream BufferedInputStream BufferedOutputStream ObjectInputStream ObjectOutputStream ByteArrayInputStream ByteArrayOutputStream 【字符流】 ...

    批量打包下载

    BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面 OutputStream outs = response.getOutputStream();// 获取文件输出IO流 BufferedOutputStream bouts = new BufferedOutputStream...

    java算法,实现压缩及解压缩

     BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));  File file = new File&#40;filePath + entry.getName(&#41;);  //加入这个的原因是zipfile读取文件是随机读取的,这就...

    IO输入输出体系.rar

    DataInputStream out = new DataInputStream(new BufferedInputStream(new FileInputStream("数据存储文件路径"))) DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(...

    实验3 输入输出流的实验.doc

    我们使用 FileOutputStream 和 FileInputStream 类来读取和写入文件,并使用 BufferedOutputStream 和 BufferedInputStream 类来提高文件流的效率。 6. 字符串常用操作方法 在实验中,我们使用了字符串常用操作...

    统计字符串-课程设计

    7. 字符数统计(满分50分) 版本1:满分15分 Write a method that counts the number of letters ...BufferedInputStream fileInput = new BufferedInputStream( new FileInputStream(new File(filename))); …. } }

    BZip2文件读取

    读取BZip2文件的jar包 new InputStreamReader(new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file)),true));

Global site tag (gtag.js) - Google Analytics