Introduction To Input And Output (I/O) 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.

import java.io.*;
class FileOutStreamEx
 
{
   
public static void main(String[] args)
     
{
       
try
         
{
           
FileOutputStream fo= new FileOutputStream("c-sharpcorner.txt");
           
String str="Thanks to visit c-sharpcorner.com";
           
byte b[]=str.getBytes();
           
fo.write(b);
           
fo.close();
           
System.out.println("you have successfuly inserted your data in your text file c-sharpcorner.txt");
          }
       
catch(Exception y)
         
{
           
System.out.println(y);
          }
      }  
  }  

Output

fig-1.jpg

After executing the program above a file "c-sharpcorner.text" is generated containng 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.

import java.io.*;
class FileInpStreamEx
 
{
   
public static void main(String[] args)
     
{
       
try
         
{
           
FileInputStream fn= new FileInputStream("c-sharpcorner.txt");
           
int k;
           
while((k=fn.read())!=-1)
           
System.out.print((char)k);
           
fn.close();
          }
       
catch(Exception y)
         
{
           
System.out.println(y);
          }
      }
  }

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.

import java.io.*;
class FileCopyEx
 
{
   
public static void main(String[] args)throws Exception
     
{
       
FileInputStream fn= new FileInputStream("c-sharpcorner.txt");
       
FileOutputStream fo= new FileOutputStream("dbtalks.txt");
       
int k=0;
       
while((k=fn.read())!=-1)
         
{
           
fo.write((byte)k);
          }
       
fn.close();
      }
  }

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

Up Next
    Ebook Download
    View all
    Learn
    View all