欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

FileInputStream和BufferedInputStream

程序员文章站 2022-07-12 11:00:39
...

 

万恶的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分钟还没完,直接强制终止。

相关标签: J#