Introduction to I/O(Input Output Stream):

Introduction to Stream:

  1. System.out: standard output stream
  2. System.in: standard input stream
  3. 3) System.err: standard error stream

INPUT-OUTPUT STREAM

Java is defining two types of streams, and they are:

  1. Byte Stream: It is used for handling input and output of byte.
  2. Character Stream: It is used for handling input and output of characters.

 

  1. Java Byte Stream Classes

It is defined by using two abstract classes at the top of the hierarchy, and they are InputStream(read data from a source) and OutputStream(writing data to a destination).

Java Input Output Stream

Let’s have an example by using code:

import java.io.*;

public class ByteStream 
{
 public static void main(String arg[]) throws IOException 
 { 
 FileInputStream fin = null;
 FileOutputStream fout = null;

try 
 {
 fin = new FileInputStream("inputfile.txt");
 fout = new FileOutputStream("outputfile.txt");

int c;
 while ((c = in.read()) != -1) 
 {
 out.write(c);
 }
 }
 finally 
 {
 if (in != null) 
 {
 in.close();
 }
 if (out != null) 
 {
 out.close();
 }
 }
 }
}

Now let’s have a file inputfile.txt with the following content

[box type=”shadow” align=”” class=”” width=””]This is a test for copy files in bytestream.[/box]

Now, compile the above program and execute it, which will result in creating outputfile.txt file with the same content that we have in inputfile.txt. So let’s put the above code in Bytestream.java file and do the following −

[box type=”shadow” align=”” class=”” width=””]$javac Bytestream.java $java Bytestream[/box]

2.Java Character Stream Classes

It is also defined by using two abstract classes at the top of the hierarchy, and they are Reader (reading character streams) and Writer (writing to character streams).

Java Character Stream Classes

Let’s have an example by using code:

import java.io. *;

public class CharacterStream 
{
 public static void main(String arg[]) throws IOException 
 {
 FileReader fin = null;
 FileWriter fout = null;
 
 try 
 {
 fin = new FileReader("inputfile.txt");
 fout = new FileWriter("outputfile.txt");

int c;
 while ((c = in.read()) != -1) 
 {

out.write(c);
 }
 }
 finally
 {
 if (in != null) 
 {
in.close();

}

if (out != null) 

{

out.close();

}

}

}

}    



 

Now let’s have a file inputfile.txt with the following content −

[box type=”shadow” align=”” class=”” width=””]This is test for copy file for Character stream.[/box]

Now, compile the above program and execute it, which will result in creating outputfile.txt file with the same content that we have in inputfile.txt. So let’s put the above code in CharacterStream.java file and do the following –

[box type=”shadow” align=”” class=”” width=””]$javac CharacterStream.java $java CharacterStream[/box]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join Free Demo Class

Let's have a chat