InputStream vs OutputStream

  • For each type of source and destination in java.io package oracle given a separate class.

InputStream Class:

  • ByteArrayInputStream
  • FileInputStream
  • FilterInputStream
    • BufferedInputStream
    • DataInputStream
    • LineNumberInputStream
    • PushbackInputStream
  • ObjectInputStream
  • PipeInputStream
  • SequenceInputStream
  • StringBufferInputStream

IputStream Class Methods:

  • Since InputStream class is the super class for all input stream classes. It has below methods with general implementation for all sub classes.

1.public int available() throws IOException :

  • Checking available bytes to read.

2.public abstract int read() throws IOException :

  • Reading byte-by-byte.

3.public int read(byte[] b) throws IOException :

  • Reading all  bytes.

4.public int read(byte[] b, int off, int length) throws IOException :

  • Reading selected range of bytes.

5.public boolean markSupported() :

  • Checking is this Stream supports marking.

6.public void mark(int readLimit) :

  •  Marking current position of stream.

7.public void reset() throws IOException :

  • Placing the control at marked place.

8.public long skip(long n) throws IOException :

  • Skip reading given range of bytes.

9.public void close() throws IOException :

  • Closing the stream.

OutputStream Class:

  • ByteArrayOutputStream
  • FileOutputStream
  • FilterOutputStream
    • BufferedOutputStream
    • DataOutputStream
    • PrintStream
  • ObjectOutputStream
  • PipeOutputStream

OutputStream class methods:

  • Since OutputStream is the super class of all binary output stream classes it has below method with general implementation for all sub classes.

1.public abstract void write(int i)throws IOException:

  • Writing one by at time to an output stream.

2.public void write(byte[] b) throws IOException :

  • Writing stream of bytes to an output stream.

3.public void write(byte []b , int off, int length) throws IOException :

  • Writing range of stream of bytes to an output stream.

4.public void flush() throws IOException :

  • Flush all bytes to destination from output Stream.

5.public void close() throws IOException :

  • Close output stream.

FileInputStream and FileOutputStream:

  • These classes are used to read and write data as byte from File.
  • Basically these two streams are used as basic data source and destination for other streams.

DataInputStream and DataOutputStream:

  • These two classes are used to read and write data as primitive data.
  • Basically these two streams are used to add capability to another input stream and output stream to read and write data as primitive types.

ObjectInputStream and ObjectOutputStream:

BufferedInputStream and BufferedOutputStream:

  •  These two classes are used to read and write data as buffers.
  • Basically these two streams are used to improve reading and writing performance of other streams.

SequenceInputStream:

  • This class is used to read data from multiple InputStreams Sequentially.

 PrintStream: 

  • PrintStream class is filter output stream class .
  • Its adds functionality to another OutputStream, namely the ability to print representations of various data values conveniently.


Basic steps to perform IO Operations:

  • Create stream class object based on source and destination.
  • Call read() method to read data from the source.
  • Call write() method to write data to the destination.
For instance , to perform IO Operations on files we must create
  • FileInputStream
  • FileOutputStream or
  • FileReader
  • FileWriter  Streams class objects
-
You might like:

Multiply two matrices

  1. package com.instanceofjava;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Multiply2Matrices{
  6.  
  7.    public static void main(String args[])
  8.    {
  9.  
  10.       int m, n, p, q, sum = 0, c, d, k;
  11.  
  12.       Scanner in = new Scanner(System.in);
  13.  
  14.       System.out.println("Enter the number of rows and columns of first matrix");
  15.  
  16.       m = in.nextInt();
  17.       n = in.nextInt();
  18.  
  19.       int first[][] = new int[m][n];
  20.  
  21.      System.out.println("Enter the elements of first matrix");
  22.  
  23.       for ( c = 0 ; c < m ; c++ )
  24.          for ( d = 0 ; d < n ; d++ )
  25.             first[c][d] = in.nextInt();
  26.  
  27.       System.out.println("Enter the number of rows and columns of second matrix");
  28.  
  29.       p = in.nextInt();
  30.       q = in.nextInt();
  31.  
  32.      if ( n != p )
  33.          System.out.println("Matrices with entered orders can't be multiplied with each other.");
  34.       else
  35.       {
  36.          int second[][] = new int[p][q];
  37.          int multiply[][] = new int[m][q];
  38.          System.out.println("Enter the elements of second matrix");
  39.  
  40.          for ( c = 0 ; c < p ; c++ )
  41.             for ( d = 0 ; d < q ; d++ )
  42.                second[c][d] = in.nextInt();
  43.  
  44.          for ( c = 0 ; c < m ; c++ )
  45.          {
  46.             for ( d = 0 ; d < q ; d++ )
  47.             {   
  48.                for ( k = 0 ; k < p ; k++ )
  49.                {
  50.                   sum = sum + first[c][k]*second[k][d];
  51.                }
  52.                multiply[c][d] = sum;
  53.                sum = 0;
  54.             }
  55.          }
  56.  
  57.          System.out.println("Multiplication of entered matrices:-");
  58.  
  59.          for ( c = 0 ; c < m ; c++ )
  60.          {
  61.             for ( d = 0 ; d < q ; d++ )
  62.                System.out.print(multiply[c][d]+"\t");
  63.             System.out.print("\n");
  64.          }
  65.  
  66.       }
  67.  
  68.    }
  69. }  

OutPut:


  1. Enter the number of rows and columns of first matrix
  2.  
  3. 2 2
  4.  
  5. Enter the elements of first matrix
  6.  
  7. 2 2
  8. 2 3
  9.  
  10. Enter the number of rows and columns of second matrix
  11.  
  12. 2 2
  13.  
  14. Enter the elements of second matrix
  15.  
  16. 1 1
  17. 1 1
  18.  
  19. Multiplication of entered matrices:-
  20.  
  21. 4    4    
  22. 5    5  


add two matrices

  1. package com.instanceofjava;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Add2Matrix
  6. {
  7.  
  8.    public static void main(String args[])
  9.    {
  10.  
  11.       int rows, cols, c, d;
  12.  
  13.       Scanner in = new Scanner(System.in);
  14.  
  15.       System.out.println("Please Enter number of rows and columns");
  16.  
  17.       rows = in.nextInt();
  18.       cols  = in.nextInt();
  19.  
  20.       int first[][] = new int[rows][cols];
  21.       int second[][] = new int[rows][cols];
  22.       int sum[][] = new int[rows][cols];
  23.  
  24.       System.out.println("Please Enter elements of first matrix");
  25.  
  26.       for (  c = 0 ; c < rows ; c++ )
  27.          for ( d = 0 ; d < cols ; d++ )
  28.             first[c][d] = in.nextInt();
  29.  
  30.       System.out.println("Please Enter elements of second matrix");
  31.  
  32.       for ( c = 0 ; c < rows ; c++ )
  33.          for ( d = 0 ; d < cols ; d++ )
  34.             second[c][d] = in.nextInt();
  35.  
  36.       for ( c = 0 ; c < rows ; c++ )
  37.          for ( d = 0 ; d < cols ; d++ )
  38.              sum[c][d] = first[c][d] + second[c][d];  //replace '+' with '-' to subtract matrices
  39.  
  40.       System.out.println("Sum of entered matrices:-");
  41.  
  42.       for ( c = 0 ; c < rows ; c++ )
  43.       {
  44.          for ( d = 0 ; d < cols ; d++ )
  45.             System.out.print(sum[c][d]+"\t");
  46.          System.out.println();
  47.       }
  48.    }

  49.  }
  50.    
     

 

Output:

  1. Please Enter number of rows and columns
  2.  
  3. 3
  4. 3
  5.  
  6. Please Enter elements of first matrix
  7.  
  8. 1 1 1
  9. 1 1 1
  10. 1 1 1
  11.  
  12. Please Enter elements of second matrix
  13.  
  14. 2 2 2
  15. 2 2 2
  16. 2 2 2
  17.  
  18. Sum of entered matrices:-
  19.  
  20. 3    3    3    
  21. 3    3    3    
  22. 3    3    3  

You might like:



IO Streams

The basic idea of  IOStreams is
  • Storing and reading data from files
  • reading data from keyword
First let us understand some basic terminology.

Persistent media:

  • The environment that allows us to store data permanently is called persistent media.
  • We can store data permanently in three places
  1. File
  2. Database
  3. Remote Computer 

Persistence:

  •  The process of storing data permanently in a persistence media.

Persistence Logic:

  • The logic that persist data in a persistence media is called persistence logic.
  • Ex: IOStreams based logic, JDBC logic, Networking based logic.

Persistence Technologies:

  •  The technology that provides API to develop persistence logic is called persistence technology.
  • Well known persistence technologies are 
  1. IOStreams : to persist data in files
  2. JDBC, EJB, Hibernate: to persist data in db
  3. Networking: to persist data in remote computer

Where can we store data permanently? 

  • In persistence medias either files or in databases.
  • Storing data in variables and arrays  is temporary. Data will be lost when a local variable goes out of scope or when the program terminates.
  • programmers use files or databases for long term storage of large amount of data. it is available even after termination of the program. We refer to data maintained on files as persistent data, because the data exists beyond the duration of the program execution.
  • To store data in files and databases  Oracle has given in  built API. We all need to do is creating the particular class object calling methods for storing and reading data from that persistence media.
  • IOStreams API is given to store and read data from files
  • JDBC API is given to store and read data from Databases. 

How java application can store or read data from a file?

  • Using stream object.

Introduction to Streams:

  • Stream is logical connection between java program and a file.
  • To store the data in the persistence media there should be a way to connect to persistence media from java application either physically or logically. Stream provides logical connection.
  • "Its a continuous flow of data between java program and persistence media"

Direction of stream flow:

  • Stream has a direction and its direction depends on the viewer's view. In java the viewer is java Application. If you look from Java Application it is sending out from Java Application.


Type of Streams:

  • Generally Streams are divided in to two types based on data flow direction.
  1. InputStream.
  2. OutPutStream.

InputStream:

  • The stream that allows data to come into the java application from the persistent media is called input Stream.

OutPutStream:

  • The stream that allows data to send out from the java application to be stored into the persistence media is called OutPutStream.
  • Basically InputStreams are used to read data from a persistence media , and Oputstreams are used to write or store  data in a persistence media from a java application

Types of Java Streams:

  • In java we are allowed to send data through streams only either in the format of bytes or characters. So based on the type of the data passed through streams .
  • In java streams are divided in to two types.
  1. Binary Streams.
  2. Character Streams.

 1.Binary Streams: 

  • The streams which read and write data in the format of  bytes is called Character streams.

 2.Character Streams:

  • The streams which read and write data in the format of  characters is called Character streams.
Java Streams

Programing Inrerview Questions on try catch

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("instance of java");    
  10.  
  11.     }
  12.  
  13. }
  14. }





2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class Demo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.      try{
  8.  
  9.     System.out.println("try block");   
  10.  
  11.     }
  12.    finally{
  13.  
  14.     System.out.println("finally block");    
  15.  
  16.   }
  17.  
  18. }
  19. }







3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.             
  8.         try {
  9.  
  10.             int a = 0;
  11.             int b = 10;
  12.             int c = b / a;
  13.  
  14.             System.out.print("try block");
  15.  
  16.         }
  17.         catch(Exception e) {
  18.  
  19.               System.out.print("catch block");
  20.  
  21.         }    
  22.  
  23. }
  24. }




String programing interview questions

1. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.charAt(3));
  9. }
  10. }



2. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str="I love java";
  8.       System.out.println(str.length());
  9. }
  10. }








3. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.       String str1="abc";
  8.       String str2="abc";
  9.  
  10.        System.out.println(str1.equals(str2));
  11. }
  12. }






4. what is the output of following program:

  1. package com.instanceofjavaforus;
  2.  
  3. public class StringDemo{
  4.  
  5.  public static void main(String[] args) {
  6.  
  7.         String str1 = "abc";
  8.         String str2 = "abc";
  9.         String str3= new String("abc");
  10.  
  11.         System.out.println("str1 == str2 ? "+(str1==str2));
  12.         System.out.println("str1 == str3 ? "+(str1==str3));
  13.         System.out.println("str1 equals str3 ? "+(str1.equals(str3)));
  14.  
  15.  
  16. }
  17. }






Select Menu