Learn to implement file handling concepts in java

0
9342

In this tutorial, we will learn about how to implement Input stream classes, Output stream classes, Reader stream classes, Writer stream classes, Buffered Input Stream classes, Buffered Output Stream classes and File System Operations in java along with suitable examples.

  • Streams : Java uses the concept of streams to represent the ordered sequence of data; a common characteristic shared by all the input or output devices, input devices like keyboard, mouse, disk, network and memory and output devices like printer, memory, network, screen and disk. A stream presents a uniform, easy to use, object-oriented interface between the program and the input or output devices. A stream in java is a path along which data flows. It has a source (of data) and a destination (for that data) as depicted in below figure. Both the source and the destination may be physical devices or programs or other streams in the same program.
  • streams

    Java Stream Classes :

    java streams

    • To learn the file handling concepts in java follow the steps given below :

    1. I/O Streams : – Java streams are classified into two basic types input stream and output stream. An input stream extracts (i.e. reads) data from the source (i.e. file) and sends it to the program. Similarly an output stream takes data from the program and sends (i.e. writes) it to the destination (file).
    2. I/O streams
      Fig.a) Reading data into a program Fig.b) Writing data to a destination.

      Java provides two kinds of byte stream classes; input stream classes and output stream classes.

      streams in java

      1. Input Stream : Input stream classes are used to read 8-bit bytes include a super class called as InputStream and a number of subclasses for supporting various input related functions. Below figure shows the class hierarchy of input stream classes.
      2. Input stream

        The super class InputStream is an abstract class and we cannot create instances of super class. So we must use the subclasses that inherit from super class. Note that the class DataInputStream extends FilterInputStream and implements the interface DataInput. So in this case the DataInputStream class implements the methods described in DataInput in addition to using the methods of InputStream class. The DataInput interface contains the below methods : –

        • readShort()
        • readBoolean()
        • readint()
        • readChar()
        • readLong()
        • readLine()
        • readFloat()
        • readDouble()
        • readUTF()

        The InputStream class defines methods for performing input functions.
        Some input stream methods are given below: –

        Input stream methods

      3. Output Stream : Output stream classes are derived from the base class OutputStream. Like InputStream, the OutputStream is an abstract class and we cannot instantiate it. The several subclasses of the OutputStream can be used for performing the output operations.
        Below figure shows the class hierarchy of output stream classes.

        output stream

        Some output stream methods are below : –

        output stream methods

        The DataOutputStream, that is similar of DataInputStream, implements the interface DataOutput, so it implements the following methods contained in DataOutput interface.

        • writeShort()
        • writeBoolean()
        • writeInt()
        • writeLong()
        • writeFloat()
        • writeDouble()
        • writeUTF()
        • writeChar()
        • writeBytes()

        Example : This example shows how InputStream and OutputStream are used in java class.


        Output :

    3. Readers and Writers : – Character stream classes that provide support for managing I/O operation on characters. These classes can be used to read and write 16-bit Unicode characters. There are two kinds of character stream classes’ reader stream classes and writer stream classes.
    4. Stream Classes

      1. Reader Stream Classes : These classes are used to read character from the files. Reader class is the super class for all other Reader classes. These classes are functionally very similar to the input stream classes, except input stream use bytes, while reader stream use characters. The Reader class is used for handled characters, so reader classes can perform all the functions implemented by the input stream classes.
      2. Hierarchy of reader stream classes: –

        10

      3. Writer Stream Classes : The writer stream classes are used to perform all output operations on files. Only difference is that while output stream classes are used for write bytes, the writer stream classes are designed to write characters. The Writer class is an abstract class which acts as a super class for all the other writer stream classes. This super class provides support for all output operations by defining methods that are exact to those in OutputStream class.
        Hierarchy of writer stream classes: –

        Writer stream classes
      4. Example : This example shows how to File Reader and Writer Stream classes are used in java.


        Output :
        Output

    5. Buffered Streams : – This class provides default methods for the bulk of the functions in the User Defined Streams protocol. The user needs to define only the methods stream-read-buffer, stream-write-buffer and stream-element-type for each subclass of buffered stream, so the default methods implement buffered I/O.
      • Buffered Input Streams : – In java.io package two streams exist in byte streams category for buffering BufferedInputStream and BufferedOutputStream. These classes are subclasses of FilterInputStream and FilterOutputStream and these are used to increase the performance with buffer by functionality. BufferedInputStream is much faster for larger data amounts and disk access. An internal buffer array is created, when the BufferedInputStream is created.
        It has two constructors: –
        i. BufferedInputStream(InputStream inputStream)
        ii. BufferedInputStream(InputStream inputstream, int bufSize)
      • Buffered Output Stream : – It is used to provide buffering to your output stream. It is also provides much faster disk access and larger data amounts. Buffer is nothing but the reserved memory block. With buffer we can write whole block of data at once.
        It has two constructors: –

        i. BufferedOutputStream(OutputStream out)
        ii. BufferedOutputStream(OutputStream out, int size)

        It inherits all methods from the FileOutputStream, few methods below: –

        a.write(byte b): – Write the specified byte to the buffered output stream
        b.write (byte [] b, int i, int j): – Write j bytes from the specified byte array starting at i to the buffered output stream.
        c.flush(): – Is used for flushes the buffered output stream
        d.close()

      Example : This example shows how to BufferedInputStream and BufferedOutputStream are used in java class.


      Output :
      13

    6. File System operations : –
      • canRead (): – Check whether the application can read the file.
      • canWrite (): – : – Check whether the application can modify the file.
      • createNewFile (): – : – – Check whether the application can modify the file.
      • delete(): – : – Delete the file or directory.
      • exists(): – : – Check whether the file or directory exists.
      • getAbsolutePath (): – : –Return the absolute pathname in string.
      • isDirectory (): – : –Check whether the file is a directory or not.
      • isHidden (): – : –Check whether the file is a hidden file or not.
      • list(): – : – Returns an array of strings naming the files and directories in the directory.

      Example : This example shows how to File System Operations is used in java class.


      Output :

    Hence, in this tutorial we successfully learnt about how to implement Input stream classes, Output stream classes, Reader stream classes, Writer stream classes, Buffered Input Stream classes, Buffered Output Stream classes and File System Operations in java along with suitable examples.

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here