📜  Java Java类

📅  最后修改于: 2022-05-13 01:54:26.884000             🧑  作者: Mango

Java Java类

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

构造函数和描述

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

方法:

  • 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() :测试此输入流是否支持 mark 和 reset 方法。
    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类