Search...

Friday, November 7, 2014

File Handling in Java

A stream (data) is a sequence of bytes that flow from source to destination.
  • We can also say that stream is an abstraction either produce or consume information.


Data may be available for reading or writing in library.                                                                                                                               In a program, we read information from input stream and write information to an output stream.

  • Package java.io.* contain many classes that allow us to define various stream with particular characteristics.
I/O Streams                                                                                         

  • Byte stream: Handle I/O of raw binary data.
  • Character stream:   Handle I/O of character data(16 bit    unicode)
  • Buffered stream:  Optimize I/O
  • Object Stream: Handle binary I/O of objects.
  • Data Stream: Handle binary I/O of primitive data type.




 

                                                                                                                                                                                                                                                                                                                                                 Reading and Writing Files:

As described earlier, A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.
Here is a hierarchy of classes to deal with Input and Output streams.
Java I/O Streams
The two important streams are FileInputStream and FileOutputStream, which would be discussed in this tutorial:

FileInputStream:

This stream is used for reading data from the files. Objects can be created using the keyword new and there are several types of constructors available.
Following constructor takes a file name as a string to create an input stream object to read the file.:
InputStream f = new FileInputStream("C:/java/hello");
Following constructor takes a file object to create an input stream object to read the file. First we create a file object using File() method as follows:
File f = new File("C:/java/hello"); 

InputStream f = new FileInputStream(f);

FileOutputStream:

FileOutputStream is used to create a file and write data into it. The stream would create a file, if it doesn't already exist, before opening it for output.
Here are two constructors which can be used to create a FileOutputStream object.
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/hello") 
Following constructor takes a file object to create an output stream object to write the file. First, we create a file object using File() method as follows:
File f = new File("C:/java/hello"); 

OutputStream f = new FileOutputStream(f);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

No comments:

Post a Comment