当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.io.BufferedInputStream用法及代码示例


BufferedInputStream 为另一个输入 stream-namely 添加了函数,即缓冲输入并支持标记和重置方法的能力。创建BufferedInputStream 时,会创建一个内部缓冲区数组。当读取或跳过流中的字节时,内部缓冲区会根据需要从包含的输入流中重新填充,一次填充多个字节。

构造函数和说明

  • BufferedInputStream(输入流中):创建 BufferedInputStream 并保存其参数(输入流)以供以后使用。
  • BufferedInputStream(输入流,int大小):创建具有指定缓冲区大小的BufferedInputStream,并保存其参数(输入流)以供以后使用。

方法:

  • int available():返回字节数的估计值
    可以从此输入流中读取(或跳过),而无需
    下次调用该输入流的方法时会阻塞。
    用法: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
    
  • 无效close():关闭此输入流并释放与该流关联的所有系统资源。
    用法:public void close()
               throws IOException
    Overrides:
    close in class FilterInputStream
    Throws:
    IOException 
    
  • 无效标记(int readlimit):标记此输入流中的当前位置。
    用法:public void mark(int readlimit)
    Overrides:
    mark in class FilterInputStream
    参数:
    readlimit - the maximum limit of bytes that can be read 
    before the mark position becomes invalid.
    
  • 布尔值markSupported():测试此输入流是否支持标记和重置方法。
    用法:public boolean markSupported()
    Overrides:
    markSupported in class FilterInputStream
    Returns:
    a boolean indicating if this stream type supports the mark and reset methods.
    
  • int read():从输入流读取下一个数据字节。
    用法:public int read()
             throws IOException
    Returns:
    the next byte of data, or -1 if the end of the stream is reached.
    Throws:
    IOException 
    
  • int 读取(字节[] b,int 关闭,int len):从给定的偏移量开始,将此 byte-input 流中的字节读取到指定的字节数组中。
    用法:public int read(byte[] b,
           int off,
           int len)
             throws IOException
    参数:
    b - destination buffer.
    off - offset at which to start storing bytes.
    len - maximum number of bytes to read.
    返回:
    the number of bytes read, or -1 if the end of the stream has been reached.
    Throws:
    IOException 
    
  • 无效reset():将此流重新定位到上次在此输入流上调用 mark 方法时的位置。
    用法:public void reset()
               throws IOException
    Overrides:
    reset in class FilterInputStream
    Throws:
    IOException
    
  • 长跳过(长n):跳过并丢弃此输入流中的 n 个字节的数据
    用法:public long skip(long n)
              throws IOException
    Parameters:
    n - the number of bytes to be skipped.
    返回:
    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.io.BufferedOutputStream



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.BufferedInputStream class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。