Introduction To Input And Output (I/O) In Java

In this article we discuss Input and Output in java.

Introduction

 
In this article, we discuss Input and Output in Java.
 

Input and Output (I/O) in Java

 
It works on input-output relation. I/O is used to access or process input and produce output based upon the input. Java uses the concept of streams that make I/O operations faster. For that Java uses the java.io package that contains all classes required for input and output operations.
 

Stream

 
A stream is a set (sequence) of data arranged in a specified manner. In Java a stream is a sequence of bytes. We call it a stream, as in a stream of water in which water flows continuously.
In the following figure, we can see how data sent in a stream format. It goes in 0/1 form.
stream.gif

The following three streams are created automatically:

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

Standard Output Stream

 
Java applications use this stream to write data to a source file or whatever you want. The output stream may be an array or a file.
In the following figure, we see the hierarchy of OutputStream.
outputstream.gif

Standard Input Stream

 
Java applications use this stream to read data from a file, the input stream may be an array or a file.
In the following figure, we see the hierarchy of InputStream.
inputstream.gif

OutputStream class

 
The OutputStream class is an abstract class, hence it is the superclass of all classes representing an object stream of bytes. It takes output bytes and sends them back to some source (file) or wherever you want.
Some methods in the OutputStream class are:
  1. void write(int) throws IOException: writes a byte to the current output stream.
  2. void write(byte[]) throws IOException: writes an array of bytes to the current output stream.
  3. void run flush() throws IOException: flushes the current output stream.
  4. void close() throws IOException: closes the current output stream.

InputStream class

 
The InputStream class is an abstract class, hence it is the superclass of all classes representing an input stream of bytes.
Some methods in the InputStream class are:
  1. public void close() throws IOException: closes the current input stream.
  2. public abstract int read() thows IOException: reads the next byte of data from the input stream. It returns -1 at the end of the file.
  3. public int available() throws IOException: returns an estimate of the number of bytes that can be read from the current input stream.

File Handling in I/O

 
Two main classes used for file handling in Java are: (1) FileOutputStream and (2) FileInputStream. They are used to read and write data in a file.
 

FileOutputStream class

 
The FileOutputStream class is an output stream class used for writing data to a specific file. If we must write primitive values to our file, then we can use this class. Instead, for character-oriented data, the FileWriter class is preferred.
 
Example
 
In this example, we create a text file "c-sharpcorner.txt" that contains the data stream that we must pass in our program.
  1. import java.io.*;  
  2. class FileOutStreamEx  
  3.   {  
  4.     public static void main(String[] args)  
  5.       {  
  6.         try  
  7.           {  
  8.             FileOutputStream fo= new FileOutputStream("c-sharpcorner.txt");  
  9.             String str="Thanks to visit c-sharpcorner.com";  
  10.             byte b[]=str.getBytes();  
  11.             fo.write(b);  
  12.             fo.close();  
  13.             System.out.println("you have successfuly inserted your data in your text file c-sharpcorner.txt");  
  14.           }  
  15.         catch(Exception y)  
  16.           {  
  17.             System.out.println(y);  
  18.           }  
  19.       }     
  20.   }    
Output
 
fig-1.jpg
 
After executing the program above a file "c-sharpcorner.text" is generated containing a string that we passed in our program. The file output is given below.
 
fig-2.jpg
 

FileInputStream class

 
The FileInputStream class is used to get input bytes from a file. Since it is also used for image data that contains a raw stream and for reading a character stream, we considered using FileReader.
 
Example
 
In this example, we have taken input from a text file "c-sharpcorner.txt" and printed the data value in our program that is shown below.
  1. import java.io.*;  
  2. class FileInpStreamEx  
  3.   {  
  4.     public static void main(String[] args)  
  5.       {  
  6.         try  
  7.           {  
  8.             FileInputStream fn= new FileInputStream("c-sharpcorner.txt");  
  9.             int k;  
  10.             while((k=fn.read())!=-1)  
  11.             System.out.print((char)k);  
  12.             fn.close();  
  13.           }  
  14.         catch(Exception y)  
  15.           {  
  16.             System.out.println(y);  
  17.           }  
  18.       }  
  19.   }  
Output
 
fig-3.jpg
 
We copied data from one file to another file,in other words in this program we get input from one file and put the records in another file.
 
Example
 
In this example, data can be copied from the current file to another file, in other words, the data stored in the c-sharpcorner.txt file should be copied to the other file dbtalks.txt.
  1. import java.io.*;  
  2. class FileCopyEx  
  3.   {  
  4.     public static void main(String[] args)throws Exception  
  5.       {  
  6.         FileInputStream fn= new FileInputStream("c-sharpcorner.txt");  
  7.         FileOutputStream fo= new FileOutputStream("dbtalks.txt");  
  8.         int k=0;  
  9.         while((k=fn.read())!=-1)  
  10.           {  
  11.             fo.write((byte)k);  
  12.           }  
  13.         fn.close();  
  14.       }  
  15.   }   
Output
 
fig-4.jpg
 
When we run our program a new file "dbtalks.txt" is generated that contains the same data as stored in c-sharpcorner.txt.
 
fig-5.jpg