Home | » | Java | » | Advance Java | » | Java File Handling |
---|
Input/Oupt (I/O) operations consist of two parts, data that is entered into a program via input source and the processed data that is returned via output source. Programs in Java perform I/O operations with the help of streams. Stream is a continuous flow of data from source to destination. In Java, stream can be considered as a path along which data terms of bits bytes, characters or words, flows. Input Stream refers to the flow of data into the program and output stream refers to the flow of data out of the program.
File Handling
Input/Output Streams | I/O Exceptions |
File Class | File Input and Output Stream |
Data Input and Output Stream | Buffered Input and Output Streams |
Reader and Writer Streams | Random Access File |
Input/Output Streams
InputStream
Input Stream and Output Stream are the two major classes of Java that control all types of data input and output. Input stream is connected to the source of information and output stream is connected to the destination for information. Input and Output stream classes are the simplest classes that provide general streaming capabilities that make the data transfer from source to destination easier. Input and output streams are the byte stream that read 8-bits bytes.
The InputStream class is an abstract class of the type input stream class. Every other input stream class is sub class of InputStream class. Various methods are defined in the InputStream class. These methods are used to perform functions such as reading data bytes, closing streams, marking position in a file and skipping data in stream and calculating the number of bytes present in a stream. These methods are:
- read() :Reads the next byte of data in an input stream. It returns -1 if it encounters the end of stream.
- available() :Returns the total number of bytes available for reading in a stream.
- skip(long n) :Discards or skips n bytes of data from the input stream.
- close() :Releases the resource connected to a stream and close the stream.
- reset :Takes the input pointer to the previously set mark or the beginning of the stream.
- makr(int byte) :Places a mark at the current point in the input stream and this marks remains until n data bytes are read.
The InputStream class includes a number of subclasses. The below Figure shows the class and subclass hierarchy of the Inputstream class.
OutputStream
The OutputStream class is also an abstract class of the type output stream class. Every other output stream class is a subclass of the OutputStream class. Various methods are defined in the OutputStream class. These methods are used for writing bytes, closing and flusing streams. Some of these methods are:
- write() :Writes output to the disk.
- close() :Closes the byte output stream.
- flush() :Forces any buffered output to be written on the disk and clears the buffers.
Similar to the InputStream class, the OutputStream class also includeds a number of subclasses. The below Figure shows the class and subclass hierarchy of the OutputStream class.
I/O Exceptions
Each class in Java class hierarchy has predefined methods that are used by its subclasses and objects to perform I/O operations. All the methods defined in the I/O stream classes throw exception.
An I/O exception is generated, whenever an error occurs at the time of execution of a program. If these exception are not handled properly, an error message is displayed on the screen and the interpreter will terminate the execution of program, in spite of occurrence of an error, you have to cacth these exceptions.
To catch these I/O related exceptions, you need to create an object of the I/O exception class and pass this object as an argument in the catch block. Structure of the try-catch block that handles exceptions is:
Some of the important I/O exception classes used commonly in program are:
- IOException :Occurs because of general I/O failure such as unable to read from a file.
- FileNotFoundException :Occurs when attempt to access non-existing file is made.
- EOFException : Occurs when an end of file is encontered unexpectedly during the input operation of reading a file.
- InterruptedIOException :Occurs when an I/O operation is interrupted.
DataInputOutputStream
The data input and output streams extend the FilterInputStream and FilterOutputStream classes correspondingly. The data input streams the DataInput interface and the data output streams extend the DataOutput interface.
The below table shows Lists the methods present in data input streams and data output streams.
Data input streams | Data output streams |
---|---|
readShort | writeShort |
readInt | writeInt |
readLong | writeLong |
readFloat | writeFloat |
readUTF | writeUTF |
readDouble | writeDouble |
readLine | writeLine |
readChar | writeChar |
readBoolean | writeBoolean |
It was such a great article.
ReplyDeleteJava Training with Placements
very nice article . there are good java file sample program
ReplyDeleteJava file handling example