Monday, 1 August 2011

File Handling

File Handling and Input/Output

java.io package
Classes related to input and output are present in the JavaTM language package java.io . Java technology uses "streams" as a general mechanism of handling data. Input streams act as a source of data. Output streams act as a destination of data.

File class
The file class is used to store the path and name of a directory or file. The file object can be used to create, rename, or delete the file or directory it represents. The File class has the following constructors -
File(String pathname); // pathname could be file or a directory name
File(String dirPathname, String filename);
File(File directory, String filename);
<!--more-->
The File class provides the getName() method which returns the name of the file excluding the directory name.
String getName();

Byte Streams
The package java.io provides two set of class hierarchies - one for handling reading and writing of bytes, and another for handling reading and writing of characters. The abstract classes InputStream and OutputStream are the root of inheritance hierarchies handling reading and writing of bytes respectively.



read and write methods
InputStream class defines the following methods for reading bytes -
int read() throws IOException
int read(byte b[]) throws IOException
int read(byte b[], int offset, int length) throws IOException
Subclasses of InputStream implement the above mentioned methods.




OutputStream class defines the following methods for writing bytes -
void write(int b) throws IOException
void write(byte b[]) throws IOException
void write(byte b[], int offset, int length) throws IOException
Subclasses of OutputStream implement the above mentioned methods.




The example below illustrates code to read a character.
//First create an object of type FileInputStream type using the name of the file.
FileInputStream inp = new FileInputStream("filename.ext");
//Create an object of type DataInputStream using inp.
DataInputStream dataInp = new DataInputStream(inp);
int i = dataInp.readInt();

Reader and Writer classes
Similar to the InputStream and OutputStream class hierarchies for reading and writing bytes, Java technology provides class hierarchies rooted at Reader and Writer classes for reading and writing characters.

A character encoding is a scheme for internal representation of characters. Java programs use 16 bit Unicode character encoding to represent characters internally. Other platforms may use a different character set (for example ASCII) to represent characters. The reader classes support conversions of Unicode characters to internal character shortage. Every platform has a default character encoding. Besides using default encoding, Reader and Writer classes can also specify which encoding scheme to use.
The Reader class hierarchy is illustrated below.
The Writer class hierarchy is illustrated below.

The table below gives a brief overview of key Reader classes.
CharArrayReader The class supports reading of characters from a character array.
InputStreamReader The class supports reading of characters from a byte input stream. A character encoding may also be specified.
FileReader The class supports reading of characters from a file using default character encoding.

The table below gives a brief overview of key Writer classes.
CharArrayWriter The class supports writing of characters from a character array.
OutputStreamReader The class supports writing of characters from a byte output stream. A character encoding may also be specified.
FileWriter The class supports writing of characters from a file using default character encoding.

The example below illustrates reading of characters using the FileReader class.
//Create a FileReader class from the file name.
FileReader fr = new FileReader("filename.txt");
int i = fr.read(); //Read a character

No comments:

Post a Comment