All IT Courses 50% Off
JAVA Tutorials

Java Input-Output Stream

Introduction to I/O(Input Output Stream):

  • Itis used to process the input and produce the output.
  • Stream concept is used in Java to make an I/O operation fast. 
  • The java.io package which contains all the classes required for input and output operations.
  • By using Java I/O API, we can perform file handling in Java.

Introduction to Stream:

  • A stream contains a sequence of data. 
  • A stream is composed of bytes in Java
  • It is also called a stream because it is like a stream of water that continues to flow.
  • In Java there are three streams are created for us automatically, and all these streams are attached with the console, and here it is:
  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

All IT Courses 50% Off

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

This is a test for copy files in bytestream.

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 −

$javac Bytestream.java $java Bytestream

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 −

This is test for copy file for Character stream.

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 –

$javac CharacterStream.java $java CharacterStream
Facebook Comments

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.

Related Articles

Back to top button