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 Java.io.BufferedInputStream.available()用法及代碼示例
- Java Java.io.BufferedInputStream.close()用法及代碼示例
- Java Java.io.BufferedInputStream.read()用法及代碼示例
- Java Java.io.BufferedInputStream.reset()用法及代碼示例
- Java Java.io.BufferedInputStream.skip()用法及代碼示例
- Java Java.io.BufferedOutputStream.flush()用法及代碼示例
- Java Java.io.BufferedOutputStream.Write()用法及代碼示例
- Java Java.io.BufferedReader.Close()用法及代碼示例
- Java Java.io.BufferedReader.mark()用法及代碼示例
- Java Java.io.BufferedReader.markSupported()用法及代碼示例
- Java Java.io.BufferedReader.read()用法及代碼示例
- Java Java.io.BufferedReader.readline()用法及代碼示例
- Java Java.io.BufferedReader.ready()用法及代碼示例
- Java Java.io.BufferedReader.reset()用法及代碼示例
- Java Java.io.BufferedReader.skip()用法及代碼示例
- Java Java.io.BufferedWriter.close()用法及代碼示例
- Java Java.io.BufferedWriter.flush()用法及代碼示例
- Java Java.io.BufferedWriter.newLine()用法及代碼示例
- Java Java.io.BufferedWriter.write()用法及代碼示例
- Java Java.io.BufferedOutputStream用法及代碼示例
- Java Java.io.BufferedReader用法及代碼示例
- Java Java.io.ByteArrayInputStream.available()用法及代碼示例
- Java Java.io.ByteArrayInputStream.close()用法及代碼示例
- Java Java.io.ByteArrayInputStream.mark()用法及代碼示例
- Java Java.io.ByteArrayInputStream.read()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.BufferedInputStream class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。