📜  Java I/O-BufferedInputStream

📅  最后修改于: 2020-09-27 02:14:48             🧑  作者: Mango

Java BufferedInputStream类

Java BufferedInputStream类用于从流中读取信息。它在内部使用缓冲机制来提高性能。

关于BufferedInputStream的要点是:

  • 当跳过或读取流中的字节时,内部缓冲区自动从包含的输入流中重新填充,一次填充多个字节。
  • 创建BufferedInputStream时,将创建一个内部缓冲区数组。

Java BufferedInputStream类声明

让我们看一下Java.io.BufferedInputStream类的声明:

public class BufferedInputStream extends FilterInputStream

Java BufferedInputStream类构造函数

Constructor Description
BufferedInputStream(InputStream IS) It creates the BufferedInputStream and saves it argument, the input stream IS, for later use.
BufferedInputStream(InputStream IS, int size) It creates the BufferedInputStream with a specified buffer size and saves it argument, the input stream IS, for later use.

Java BufferedInputStream类方法

Method Description
int available() It returns an estimate number of bytes that can be read from the input stream without blocking by the next invocation method for the input stream.
int read() It read the next byte of data from the input stream.
int read(byte[] b, int off, int ln) It read the bytes from the specified byte-input stream into a specified byte array, starting with the given offset.
void close() It closes the input stream and releases any of the system resources associated with the stream.
void reset() It repositions the stream at a position the mark method was last called on this input stream.
void mark(int readlimit) It sees the general contract of the mark method for the input stream.
long skip(long x) It skips over and discards x bytes of data from the input stream.
boolean markSupported() It tests for the input stream to support the mark and reset methods.

Java BufferedInputStream的示例

让我们看一个使用BufferedInputStream读取文件数据的简单示例:

package com.javatpoint;
 
import java.io.*;
public class BufferedInputStreamExample{  
 public static void main(String args[]){  
  try{  
    FileInputStream fin=new FileInputStream("D:\\testout.txt");  
    BufferedInputStream bin=new BufferedInputStream(fin);  
    int i;  
    while((i=bin.read())!=-1){  
     System.out.print((char)i);  
    }  
    bin.close();  
    fin.close();  
  }catch(Exception e){System.out.println(e);}  
 }  
}

在这里,我们假设您在“ testout.txt”文件中包含以下数据:

javaTpoint

输出:

javaTpoint