Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
SequenceInputStream
3.
Declaration
4.
Working of SequenceInputStream
5.
Constructors for SequenceInputStream
6.
Methods of SequenceInputStream
7.
Examples 
7.1.
Code 1
7.2.
Code 2
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024

Sequence Input Stream

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In this article, we shall be discussing SequenceInputStream. Hence moving forward to the main topic, we shall brush up on some previous concepts. Like, what is an input Stream? It’s a powerful concept that gives all input and output operations. The stream can be defined as the logical relationship between a Java program and a file. Stream divided based on data flow direction.

SequenceInputStream

SequenceInputStream can be defined as a single input stream that combines two or more input streams in a particular order. This reads data from two or more input streams in a sequence, one after the other. This stream follows a specific order for reading the input streams. 

Also see, Hashcode Method in Java

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Declaration

public class SequenceInputStream extends InputStream

Working of SequenceInputStream

This input stream first reads the data(in bytes) from the first stream sequentially. Then all the data sequentially from the second stream, from the third, and so on.

While ending the first stream, the file is reached at a closed state; then, the following data comes from the input stream. This process continues until the file is reached at the end of the input streams.

Constructors for SequenceInputStream

Methods of SequenceInputStream

Examples 

File contents of input.txt

Hello Coders

File contents of out.txt

Welcome to Coding Ninjas.

Code 1

import java.io.*;
public class SequenceInput1 {


	public static void main(String args[])throws Exception{    
		FileInputStream input1=new FileInputStream("input.txt");    
		FileInputStream input2=new FileInputStream("out.txt");    
		SequenceInputStream inst=new SequenceInputStream(input1, input2);    
		int j;    
		while((j=inst.read())!=-1){    
			System.out.print((char)j);    
		}    
		inst.close();    
		input1.close();    
		input2.close();    
	}    
}    

Output

Hello Coders.
Welcome to Coding Ninjas.

In this example, we have taken SequenceInputStream, and we have input the two files from the user and printed the contents of the file.

File contents of input.txt

Hello Coders

File contents of out.txt

Welcome to Coding Ninjas.


Practice by yourself on java online compiler.

Code 2

import java.io.*;
public class SequenceInput2 
{
	public static void main(String[] args) throws IOException
	{
		FileInputStream file1 = new FileInputStream("input.txt");    
		FileInputStream file2 = new FileInputStream("input.txt");  
		FileOutputStream fos = new FileOutputStream("out.txt");  


		SequenceInputStream file3 = new SequenceInputStream(file1,file2);    
  		
		BufferedInputStream bis = new BufferedInputStream(file3);
		BufferedOutputStream bos = new BufferedOutputStream(fos);
     		
		int i;    
		while((i = bis.read()) != -1){    
			bos.write((char)i);    
		}    
		bis.close();    
		bos.close();    
		fos.close();
		file1.close(); 
		file2.close();
		file3.close();
		System.out.println("Successfully written...");
	}
}

Output

Successfully written…

In this example, we have taken two different files and tried as an input, and we also used both files to write the content on the third file. After validating certain conditions, we have printed a message written on the third file is successful.

Check out this problem - Shortest Common Supersequence, Duck Number in Java

FAQs

  1. What is a SequenceInputStream?
    It represents the logical concatenation of other input streams. This begins on the ordered collection of input streams and reads from the first one until the end of the file is encountered.
     
  2. How to read data from multiple files in Java SequenceInputStream?
    If we want to read the data from multiple files, we need to use Enumeration. It can be obtained by calling the elements() method.
     
  3. How to create a new InputStream in Java?
    Java.io is used to create SequenceInputStream class. It creates a new Input stream by reading the data of two input streams in order, first s1 then s2.

Key Takeaways

In this article section, we have discussed the topic of SequenceInputStream. We briefly introduced the topic and explained the declaration. We also discussed its working along with methods and constructors used in it. We also took a few examples regarding the topic. I hope that this topic might enhance your knowledge, and it might have eased your coding journey. If you want to explore more topics and practice more, please visit Top Apple coding Interview Questions. Do upvote our blog to help other ninjas grow. Happy Coding!”

Previous article
FilterInput/OutputStream
Next article
BufferedReader Vs Scanner Class In Java
Live masterclass