爪哇。我。Java中的BufferInputStream类

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

null

构造函数和描述

  • BufferedInputStream(输入流输入): 创建一个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 
    
  • 无效标记(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.
    
  • 支持布尔标记() 测试此输入流是否支持标记和重置方法。
    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(字节[]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(): 将此流重新定位到上次在此输入流上调用mark方法时的位置。
    Syntax:public void reset()
               throws IOException
    Overrides:
    reset in class FilterInputStream
    Throws:
    IOException
    
  • 长跳跃(长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中的BufferedOutputStream类

本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享