GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - SequenceInputStream Class
Selected Reading
Java - SequenceInputStream ClassIntroductionThe Java SequenceInputStream class represents the logical concatenation of other input streams. It starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams. Class declarationFollowing is the declaration for Java.io.SequenceInputStream class − public class SequenceInputStream extends InputStream Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - available() before and after reading dataThe following example shows the usage of SequenceInputStream available() method. SequenceInputStreamDemo.javapackage 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 sis = new SequenceInputStream(input1, input2);
System.out.println("Available before reading: " + sis.available());
// Read some data
sis.read(); // Read 1 byte
sis.read(); // Read another byte
System.out.println("Available after reading 2 bytes: " + sis.available());
sis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− Available before reading: 5 Available after reading 2 bytes: 3 Explanation
Example - Properly closing a SequenceInputStream after readingThe following example shows the usage of SequenceInputStream close() method. SequenceInputStreamDemo.javapackage 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("Java".getBytes());
ByteArrayInputStream input2 = new ByteArrayInputStream("Stream".getBytes());
SequenceInputStream sis = new SequenceInputStream(input1, input2);
int data;
while ((data = sis.read()) != -1) {
System.out.print((char) data);
}
sis.close(); // Closes SequenceInputStream and both ByteArrayInputStreams
System.out.println("\nStream closed successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− JavaStream Stream closed successfully. Explanation
Example - Read one byte at a time from two combined stringsThe following example shows the usage of SequenceInputStream read() method. SequenceInputStreamDemo.javapackage 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();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− ABC123 Explanation
Advertisements
| ||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |