DataInputStream DataOutputStream File Copy


DataInputStream and DataOutputStream are high-level classes as they are the subclasses of FilterInputStream and FilterOutputStream. These streams' extra functionality is that they can read or write integers, doubles and lines at a time, instead of byte by byte. This increases the performance to some extent; instead of reading and writing byte by byte with FileInputStream and FileOutputStream.

Storing of data of integers, floats and doubles etc. is platform-dependent. These streams take care of these underlying methodologies and abstracts it from the programmer.

Performance Advantage with DataInputStream and DataOutputStream

These streams improve the speed of copying. To achieve performance, the DataInputStream class comes with some special methods like readInt(), readDouble() and readLine() etc. Similarly, the DataOutputStream comes with methods like writeInt(), writeDouble() and writeBytes() etc. These methods can read and write an integer, a double and a line as a whole at a time instead of byte by byte.

File Copying with DataInputStream and DataOutputStream

In the following code, a line at a time is read and written to a file using DataInputStream DataOutputStream File Copy.

import java.io.*;
public class DISandDOS1
{
  public static void main(String args[]) throws IOException
  {
	                                                       // reading side
    FileInputStream fistream = new FileInputStream("pqr.txt"); // a low-level byte stream
    DataInputStream distream = new DataInputStream(fistream);    //   a high-level byte stream
                      				              // writing side
    FileOutputStream fostream = new FileOutputStream("def.txt"); // a low-level byte stream
    DataOutputStream dostream = new DataOutputStream(fostream); // a high-level byte stream

    int numLines = 0;
    String str;
    String lineEnd = System.getProperty("line.separator");
    while( ( str = distream.readLine() ) != null )
    {
      dostream.writeBytes(str);    // writes in the destination file, def.txt
      dostream.writeBytes(lineEnd); // gives a new line in def.txt
      System.out.println(str);      // prints at DOS prompt
      numLines++;
    }
    System.out.println("No. of lines: " + numLines);
    dostream.close();   fostream.close();
    distream.close();    fistream.close();
  }
}

DataInputStream DataOutputStream File Copy
Output screenshot on DataInputStream DataOutputStream File Copy

FileInputStream fistream = new FileInputStream("pqr.txt");
DataInputStream distream = new DataInputStream(fistream);

FileInputStream constructor is used to open the file pqr.txt in read mode. The object of FileInputStream, fistream, is passed to the DataInputStream constructor. FileInputStream is a low-level stream and DataInputStream is high-level stream and as per the rules of chaining, the passing is accepted.

FileOutputStream fostream = new FileOutputStream("xyz.txt"); DataOutputStream dostream = new DataOutputStream(fostream);

Similarly, the FileOutputStream opens the file xyz.txt in write mode and its object is passed to the constructor of DataOutputStream.

By this chaining what we are achieving? Using readLine() method of DataInputStream, a line from source file pqr.txt, at at a time is read instead of byte after byte. Similarly, using writeBytes() method of DataOutputStream, a line at a time is written to the destination file, xyz.txt. This increases the performance (speed of copying) considerably.

String lineEnd = System.getProperty("line.separator");

The new line character is operating system dependent. It is read programmatically from the underlying operating system and put. The static method getProperty("line.separator") of System class returns the new line character of the operating system where the programming is executing.

while( ( str = distream.readLine() ) != null )

The readLine() method of DataInputStream reads a line from the file and returns it as a string, irrespective of what the line contains like letters, digits and special characters etc. The method returns null if EOF is reached.

dostream.writeBytes(str);

The DataOutputStream method writeBytes() writes the string str to the destination file at a time.

A counter is maintained to know the number of lines in the source file.

Deprecation message

When we compile this program, we obtain a deprecation message because readLine() method of DataInputStream is deprecated (see the above screen shot). The readLine() is found problematic by the designers as it is unable to convert bytes to characters properly. So, any program using readLine() method of DataInputStream raises deprecation message.

Java designers advice to use the readLine() method of BufferedReader in place of readLine() of DataInputStream.

Leave a Comment

Your email address will not be published.