Java中的Java.io.BufferedInputStream类

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)

Java中的Java.io.BufferedInputStream类

BufferedInputStream将功能添加到另一个输入流 - 即缓冲输入和支持标记和重置方法的功能。当创建BufferedInputStream时,会创建一个内部缓冲区数组。当流中的字节被读取或跳过时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次处理多个字节。
BufferedInputStream继承于FilterInputStream,提供缓冲输入流功能。缓冲输入流相对于普通输入流的优势是,它提供了一个缓冲数组,每次调用read方法的时候,它首先尝试从缓冲区里读取数据,若读取失败(缓冲区无可读数据),则选择从物理数据源(譬如文件)读取新数据(这里会尝试尽可能读取多的字节)放入到缓冲区中,最后再将缓冲区中的内容部分或全部返回给用户.由于从缓冲区里读取数据远比直接从物理数据源(譬如文件)读取速度快。

构造函数和说明

  • BufferedInputStream(InputStream in):创建一个BufferedInputStream,并将其参数(输入流)保存,供以后使用。
  • BufferedInputStream(InputStream in,int size):使用指定的缓冲区大小创建一个BufferedInputStream,并将其参数(输入流)保存在中,供以后使用。

方法:

  • int available():
    Syntax:public int available()
                  throws IOException
    Returns:
    an estimate of the number of bytes that can be 
    read (or skipped over) from this input stream without blocking.
    Throws:
    IOException
  • void close():关闭此输入流并释放与该流关联的所有系统资源。
    Syntax:public void close()
               throws IOException
    Overrides:
    close in class FilterInputStream
    Throws:
    IOException
  • void mark(int readlimit):标记此输入流中的当前位置。
    Syntax:public void mark(int readlimit)
    Overrides:
    mark in class FilterInputStream
    Parameters:
    readlimit - the maximum limit of bytes that can be read 
    before the mark position becomes invalid.
  • boolean markSupported():测试此输入流是否支持标记和重置方法。
    Syntax:public boolean markSupported()
    Overrides:
    markSupported in class FilterInputStream
    Returns:
    a boolean indicating if this stream type supports the mark and reset methods.
  • int read():从输入流中读取下一个字节的数据。
    Syntax:public int read()
             throws IOException
    Returns:
    the next byte of data, or -1 if the end of the stream is reached.
    Throws:
    IOException
  • int read(byte [] b,int off,int len):从给定偏移量开始,将字节输入流中的字节读入指定的字节数组中。
    Syntax:public int read(byte[] b,
           int off,
           int len)
             throws IOException
    Parameters:
    b - destination buffer.
    off - offset at which to start storing bytes.
    len - maximum number of bytes to read.
    Returns:
    the number of bytes read, or -1 if the end of the stream has been reached.
    Throws:
    IOException
  • void reset():将此流重新定位到上次在此输入流上调用标记方法时的位置。
    Syntax:public void reset()
               throws IOException
    Overrides:
    reset in class FilterInputStream
    Throws:
    IOException
  • long skip(long n):跳过并丢弃来自此输入流的n个字节的数据
    Syntax:public long skip(long n)
              throws IOException
    Parameters:
    n - the number of bytes to be skipped.
    Returns:
    the actual number of bytes skipped.
    Throws:
    IOException

程序:

// Java program to demonstrate working of BufferedInputStream
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

// Java program to demonstrate BufferedInputStream methods
class BufferedInputStreamDemo
{
    public static void main(String args[]) throws IOException
    {
        // attach the file to FileInputStream
        FileInputStream fin = new FileInputStream("file1.txt");

        BufferedInputStream bin = new BufferedInputStream(fin);

        // illustrating available method
        System.out.println("Number of remaining bytes:" +
                                            bin.available());

        // illustrating markSupported() and mark() method
        boolean b=bin.markSupported();
        if (b)
            bin.mark(bin.available());

        // illustrating skip method
        /*Original File content:
        * This is my first line
        * This is my second line*/
        bin.skip(4);
        System.out.println("FileContents :");

        // read characters from FileInputStream and
        // write them
        int ch;
        while ((ch=bin.read()) != -1)
            System.out.print((char)ch);

        // illustrating reset() method
        bin.reset();
        while ((ch=bin.read()) != -1)
            System.out.print((char)ch);

        // close the file
        fin.close();
    }
}

输出:

Number of remaining bytes:47
FileContents :
 is my first line
This is my second line
This is my first line
This is my second line
  •   本文标题:Java中的Java.io.BufferedInputStream类 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4265.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读