SequenceInputStream 類允許您連接多個InputStream。它一一讀取流的數據。它從輸入流的有序集合開始,從第一個輸入流讀取,直到到達文件末尾,然後從第二個輸入流讀取,依此類推,直到最後一個包含的輸入流到達文件末尾。
構造函數和說明
- 序列輸入流(枚舉e):初始化一個新創建的SequenceInputStream,它必須是一個生成類型為InputStream的對象的Enumeration。
- 序列輸入流(輸入流 s1,InputStream s2):通過記住兩個參數來初始化新創建的SequenceInputStream,這兩個參數將按順序讀取,首先是 s1,然後是 s2。
重要方法:
- read :從此輸入流讀取下一個數據字節。
用法: public int read() throws IOException 返回: the next byte of data, or -1 if the end of the stream is reached. Throws: IOException - if an I/O error occurs.
- 讀取(字節[] b,int關閉,int長度):從此輸入流中讀取最多 len 個字節的數據到數組中
用法: public int read(byte[] b, int off, int len) throws IOException Overrides: read in class InputStream Parameters: b - the buffer into which the data is read. off - the start offset in array b at which the data is written. len - the maximum number of bytes read. Returns: int the number of bytes read. Throws: NullPointerException - If b is null. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off IOException - if an I/O error occurs.
- available :返回可以從當前基礎輸入流中讀取(或跳過)的字節數的估計值,而不會被當前基礎輸入流的方法的下一次調用所阻塞。
Syntax : public int available() throws IOException Overrides: available in class InputStream 返回:an estimate of the number of bytes that can be read (or skipped over) from the current underlying input stream without blocking or 0 if this input stream has been closed by invoking its close() method Throws: IOException - if an I/O error occurs.
- close:關閉此輸入流並釋放與該流關聯的所有係統資源。
Syntax : public void close() throws IOException Overrides: close in class InputStream Throws: IOException - if an I/O error occurs.
以下是 SequenceInputStream 類的示例,它實現了一些重要的方法。
程序:
//Java program to demonstrate SequenceInputStream
import java.io.*;
import java.util.*;
class SequenceISDemp
{
public static void main(String args[])throws IOException
{
//creating the FileInputStream objects for all the following files
FileInputStream fin=new FileInputStream("file1.txt");
FileInputStream fin2=new FileInputStream("file2.txt");
FileInputStream fin3=new FileInputStream("file3.txt");
//adding fileinputstream obj to a vector object
Vector v = new Vector();
v.add(fin);
v.add(fin2);
v.add(fin3);
//creating enumeration object by calling the elements method
Enumeration enumeration = v.elements();
//passing the enumeration object in the constructor
SequenceInputStream sin = new SequenceInputStream(enumeration);
// determine how many bytes are available in the first stream
System.out.println("" + sin.available());
// Estimating the number of bytes that can be read
// from the current underlying input stream
System.out.println( sin.available());
int i = 0;
while((i = sin.read())! = -1)
{
System.out.print((char)i);
}
sin.close();
fin.close();
fin2.close();
fin3.close();
}
}
輸出:
19 This is first file This is second file This is third file
注意:該程序不會在在線 IDE 上運行,因為沒有與其關聯的文件。
相關用法
- Java Java.io.SequenceInputStream.available()用法及代碼示例
- Java Java.io.SequenceInputStream.close()用法及代碼示例
- Java Java.io.SequenceInputStream.read()用法及代碼示例
- Java Java.io.StreamTokenizer.commentChar()用法及代碼示例
- Java Java.io.StreamTokenizer.eolIsSignificant()用法及代碼示例
- Java Java.io.StreamTokenizer.lineno()用法及代碼示例
- Java Java.io.StreamTokenizer.lowerCaseMode()用法及代碼示例
- Java Java.io.StreamTokenizer.nextToken()用法及代碼示例
- Java Java.io.StreamTokenizer.ordinaryChar()用法及代碼示例
- Java Java.io.StreamTokenizer.ordinaryChars()用法及代碼示例
- Java Java.io.StreamTokenizer.parseNumbers()用法及代碼示例
- Java Java.io.StreamTokenizer.pushBack()用法及代碼示例
- Java Java.io.StreamTokenizer.quoteChar()用法及代碼示例
- Java Java.io.StreamTokenizer.resetSyntax()用法及代碼示例
- Java Java.io.StreamTokenizer.toString()用法及代碼示例
- Java Java.io.StreamTokenizer.whitespaceChars()用法及代碼示例
- Java Java.io.StreamTokenizer.wordChars()用法及代碼示例
- Java Java.io.StringReader.close()用法及代碼示例
- Java Java.io.StringReader.mark()用法及代碼示例
- Java Java.io.StringReader.markSupported()用法及代碼示例
- Java Java.io.StringReader.read()用法及代碼示例
- Java Java.io.StringReader.ready()用法及代碼示例
- Java Java.io.StringReader.reset()用法及代碼示例
- Java Java.io.StringReader.skip()用法及代碼示例
- Java Java.io.StringWriter.append()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.SequenceInputStream in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。