Difference Between InputStream and OutputStream in Java


InputStream and OutputStream both are the abstraction process which can be implemented to access the low level data sets as pointers . They are the signified APIs to specify a particular data sequence of an operation by following some individual steps. The InputStream rearranges a data set as an ordered stream of bytes which actually reads the data from a file or received by using a network system. After ending the stream process it will return -1 as an integer value as Java does not contain any unsigned byte as data type. An OutputStream is a streamed process which mainly accepts some output bytes to sink them up into a string. It is the least type of method which writes one byte as output for the given socket. Here in this article, we will find the possible differences between these both streams with some real time processes.

Input

FileOutputStream fileOut = new FileOutputStream (" ARBRDD . txt " ) ;

Output

File is successfully updated today ! !

Differences Between InputStream and OutputStream

InputStream

OutputStream

Reads the data from a file, network or in a memory by using read () , read ( byte [] ) , and read ( byte [] , int, int ) functions.

It is designed for to write a data on destination to a file, network or in a memory by using write ( int ) , write ( byte [] ) , and write ( byte [], int, int ).

Data flows from source to application to create an instance.

Able to write a data by using bytes, characters or objects with the specific subclasses.

It can use FileInputStream or ByteArrayInputStream process to concatenate the existing data.

It can use FileOutputStream or ByteArrayOutputStream process to concatenate the existing data.

Methods Used

Using the FileInputStream and FileOutputStream Functions.

Algorithm

In this algorithm, we are going to explain the working process of the stream classes in an ordered manner. First we will declare and set the insertion order with a timer class. Further by iterating the length traversal we will evaluate the outcome of the input.

  • Step 1 − Start the process.

  • Step 2 − Declare input output stream.

  • Step 3 − Import the built-in classes and declared functions.

  • Step 4 − Declare a public class.

  • Step 5 − Set the functions.

  • Step 6 − Go for the insertion order.

  • Step 7 − Declare an array list and populate it.

  • Step 8 − Declare the Set values.

  • Step 9 − Print the values by following the insertion manner.

  • Step 10 − Declare a loop to iterate the process.

  • Step 11 − Set the timer value.

  • Step 12 − Run the process ang get the output value.

  • Step 13 − Terminate the process.

Syntax

The syntax will state how to declare a process as null to mark and skip as per the boolean process. After that we will reset the process to trace the stack value by imposing the exception class.

public class NewClass{
   public static void main ( String [] args ) throws Exception{
      InputStream PROCESS ARBRDD  = null;
      try {
         PROCESS ARBRDD  = new FileInputStream ( " FILE NAME . txt " ) ;
         
         // PRINT METHOD
         PROCESS ARBRDD . mark ( 0 ) ;
         PROCESS ARBRDD . skip ( 1 ) ;
         
         // PRINT METHOD
         boolean check = PROCESS ARBRDD . markSupported () ;
         if ( PROCESS ARBRDD . markSupported () ){
            PROCESS ARBRDD . reset () ;
            // PRINT METHODS
         }
         else
         // PRINT METHODS
      }
      catch ( Exception excpt ){
         excpt . printStackTrace () ;
      }
      finally{
         if ( PROCESS ARBRDD != null ){
            PROCESS ARBRDD . close () ;
         }
      }
   }
}

Using the FileInputStream and FileOutputStream Method

In this approach we have used the FileInputStream and FileOutputStream method on a set to perform the streaming process.

Example

In this code we have taken a text file to print a string into it with the Java stream functions. In this process we have declared a catch channel to bypass the exceptions.

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
import java.util.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class ARBRDD{
   public static void main ( String args [] ){
      try {
         FileOutputStream fileOut
            = new FileOutputStream ( "ARBRDD . txt " ) ;
         String s = " KOL DHKA " ;
         byte b [] = s . getBytes () ;
         fileOut . write ( b ) ;
         fileOut . close () ;
         System.out.println ("file is successfully updated today !! " ) ;
      }
      catch ( Exception e ) {
         System.out.println ( e ) ;
      }
   }
}

Output

file is successfully updated today!!

Conclusion

The ByteArray streams are the process to write some data into a Byte array. Thus , in this topic, we have performed the different stream functions to make a connection between data and code.

Updated on: 19-Oct-2023

536 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements