This section contains the Introduction to file input and output in java.
Introduction to file input and output
A stream can be defined as a sequence of data. To write data to the target,
we use OutputStream and
to read data from source we use InputStream.
Here is a hierarchy of classes to deal with Input and Output streams :
The two important streams are FileInputStream and FileOutputStream which would be discussed here :
FileInputStream
To read data from the file, we use this type of stream. Using 'new' keyword,
you can create Objects. In spite
of that, there are several types of
constructors available.
Following constructor takes a file name as a string to create an input stream object to write the file :
InputStream f = new FileInputStream("C:/java/NewFile");
First we create a file object using File() method. Following constructor
takes a file object to create an input
stream object to read the file :
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
After creating InputStream object, you can perform additional operations on the stream using given below methods :
Methods | Description |
public void close() throws IOException{} | This method closes the file output stream. Releases any system
resources associated with the file. Throws an IOException. |
protected void finalize()throws IOException {} | This method cleans up the connection to the file. Ensures that the
close method of this file output stream is called when there are no more references to this stream. Throws an IOException. |
public int read(int r)throws IOException{} | This method reads the specified byte of data from the InputStream.
Returns an int. Returns the next byte of data and -1 will be returned if it's end of file. |
public int read(byte[] r) throws IOException{} | This method reads r.length bytes from the input stream into an
array. Returns the total number of bytes read. If end of file -1 will be returned. |
public int available() throws IOException{} | Gives the number of bytes that can be read from this file input
stream. Returns an int. |
FileOutputStream
FileOutputStream is basically used to write data into file. But it would
create a file, if it doesn't already exist,
before opening it for output.
Following constructor takes a file name as a string to create an input stream object to write the file :
OutputStream f = new FileOutputStream("C:/java/NewFile
")
First we create a file object using File( ) method. Following constructor takes a file object to create an output stream object to write the file.
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Once you have OutputStream object in hand then there is a list of helper methods which can be used to write to stream or to do other operations on the stream.
Methods | Description |
public void close() throws IOException{} | This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException. |
protected void finalize()throws IOException {} | This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException |
public void write(int w)throws IOException{} | This methods writes the specified byte to the output stream. |
public void write(byte[] w) | Writes w.length bytes from the mentioned byte array to the OutputStream. |
Example :
Given below example to demonstrate InputStream and OutputStream :
import java.io.*; public class FileIODemo { public static void main(String args[]) { try { byte bWrite[] = { 11, 21, 3, 40, 5 }; OutputStream os = new FileOutputStream("C:/Demo.txt"); for (int x = 0; x < bWrite.length; x++) { os.write(bWrite[x]); // writes the bytes } os.close(); InputStream is = new FileInputStream("C:/Demo.txt"); int size = is.available(); for (int i = 0; i < size; i++) { System.out.print(is.read() + " "); } is.close(); } catch (IOException e) { System.out.print("Exception"); } } }
Output :
C:\Program Files\Java\jdk1.6.0_18\bin>javac FileIODemo.java C:\Program Files\Java\jdk1.6.0_18\bin>java FileIODemo 11 21 3 40 5 |
[ 0 ] Comments