Core Java - [Java File Handling]

♠ Posted by Unknown in at 23:26

Java Stream Classes


The java.io package a large number of stream classes that provide capabilities for processing all types of data. These classes may be categorized into two groups on the data type on which they operate.


1. Byte stream classes that provide support for handling I/O operations on bytes.
2. Character stream classes that provide support for managing I/O operations on characters.

These two groups may further be classified based on their purpose.



Image: Java Stream Classes

Byte Stream Classes :

Byte stream classes have been designed to provide functional features for creating and manipulating streams and files for reading and writing bytes. Since the streams are unidirectional, they can transmit bytes in only one direction and therefore Java provides two kinds of byte stream classes : input stream classes and output stream classes.

Input Stream Classes :

The super class InputStream is an abstract class and therefore we cannot create instances of this class. Rather, we must use the subclasses that inherit from this class. The InputStream class define methods for performing input functions such as,

read(), read(byte b[]), read(byte b[], int n, int m), available(), skip(n), reset(), and close() methods.

 Output Stream Classes:

Output Stream classes are derived from the base class OutputStream. The OutputStream includes methods that are designed to perform the following tasks:

write(), write(byte b[]), write(byte b[], int n, int m), close(), flush()

Image: Java Output Stream Class

Example of reading and writing bytes from one file to another file.

import java.io.*;

public class ByteFileExample
{
            public static void main(String[] args)
            {
                        File inFile = new File("Input.dat");
                        File outFile = new File("Output.dat");
                        FileInputStream ins = null; //Create File Stream for Reading
                        FileOutputStream outs = null; //Create File Stream for Writing
                       
                        byte byteRead;
                        try
                        {
                                    ins = new FileInputStream(inFile);
                                    outs = new FileOutputStream(outFile);
                                   
                                    while((byteRead = (byte) ins.read()) != -1)
                                                outs.write(byteRead);
                        }
                        catch(IOException e)
                        {
                                    System.out.println(e);
                                    System.exit(-1);
                        }
                        finally
                        {
                                    try
                                    {
                                                ins.close();
                                                outs.close();
                                    }
                                    catch(IOException e)
                                    {}
                        }          
            }

}

Character Stream Classes :

Character stream classes were not a part of the language when it was relaeased in 1995. They were added later when the version 1.1 was announced. Character streams can be used to read and write 16 bits unicode characters. There are two kinds of character stream classes, namely, reader stream classes and writer stream classes.

Reader Stream Classes :


Image: Java Reader Class

Reader stream classes are designed to read character from the files. Reader class is the base class for all other classes in his group. The classes and methods are functionally very similar to the input stream classes.

Writer Stream Classes : 


Image: Java Writer Class
Like output stream classes, the writer stream classes are designed to perform all output operations on files. only difference is that while output stream classes are designed to write bytes, the writer stream classes are designed to write characters.

Example of reading and writing characters from one file to another file.

import java.io.*;

public class CharacterFileExample
{1
            public static void main(String[] args)
            {
                        File inFile = new File("Input.dat");
                        File outFile  new File("Output.dat");
                        FileReader ins = null; //Create File Stream for Reading
                        FileWriter outs = null; //Create File Stream for Writing
                       
                        try
                        {
                                    ins = new FileReader(inFile);
                                    outs = new FileWriter(outFile);
                                   
                                    int ch;
                                    while((ch = ins.read()) != -1)
                                                outs.write(ch);
                        }
                        catch(IOException e)
                        {
                                    System.out.println(e);
                                    System.exit(-1);
                        }
                        finally
                        {
                                    try
                                    {
                                                ins.close();
                                                outs.close();
                                    }
                                    catch(IOException e)
                                    {}
                        }          
            }
}



2 comments:

Thanks for Sharing this valuble information and itis useful for me and CORE JAVA learners.We also provides the best Online CORE JAVA Training classes.

Nice Article. Very Helpful. You can follow this article also Java File handling tutorials with examples

http://www.javaproficiency.com/2015/02/file-handling-in-java.html

Post a Comment