
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - SequenceInputStream read() method
Description
The Java SequenceInputStream read() method reads the next byte of data from this input stream. The byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
Declaration
Following is the declaration for java.io.SequenceInputStream.read() method.
public int read()
Parameters
NA
Return Value
This method returns the next byte of data, or -1 if the end of the stream is reached.
Exception
IOException − If an I/O error occurs.
Example - Usage of SequenceInputStream read() method
The following example shows the usage of SequenceInputStream read() method.
SequenceInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.SequenceInputStream; public class SequenceInputStreamDemo { public static void main(String[] args) { // create two new strings with 5 characters each String s1 = "Hello"; String s2 = "World"; // create 2 input streams byte[] b1 = s1.getBytes(); byte[] b2 = s2.getBytes(); ByteArrayInputStream is1 = new ByteArrayInputStream(b1); ByteArrayInputStream is2 = new ByteArrayInputStream(b2); // create a new Sequence Input Stream SequenceInputStream sis = new SequenceInputStream(is1, is2); try { // read 10 characters, 5 from each stream for (int i = 0; i < 10; i++) { char c = (char) sis.read(); System.out.print(c); } // change line System.out.println(); // close the streams sis.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
HelloWorld
Example - Read one byte at a time from two combined strings
The following example shows the usage of SequenceInputStream read() method.
SequenceInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.SequenceInputStream; import java.io.IOException; public class SequenceInputStreamDemo { public static void main(String[] args) { try { ByteArrayInputStream stream1 = new ByteArrayInputStream("ABC".getBytes()); ByteArrayInputStream stream2 = new ByteArrayInputStream("123".getBytes()); SequenceInputStream seqStream = new SequenceInputStream(stream1, stream2); int byteData; while ((byteData = seqStream.read()) != -1) { System.out.print((char) byteData); // Cast int to char for printing } seqStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
ABC123
Explanation
Combines "ABC" and "123" into one stream.
Reads each byte sequentially and prints it as a character.
Example - Read bytes and count total characters read
The following example shows the usage of SequenceInputStream read() method.
SequenceInputStreamDemo.java
package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.SequenceInputStream; import java.io.IOException; public class SequenceInputStreamDemo { public static void main(String[] args) { try { ByteArrayInputStream input1 = new ByteArrayInputStream("Hello".getBytes()); ByteArrayInputStream input2 = new ByteArrayInputStream("World".getBytes()); SequenceInputStream seqStream = new SequenceInputStream(input1, input2); int byteData; int count = 0; while ((byteData = seqStream.read()) != -1) { count++; } System.out.println("Total characters read: " + count); seqStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Total characters read: 10
Explanation
The two strings "Hello" (5 chars) and "World" (5 chars) total 10 bytes.
The program reads all bytes and counts them.