DataInputStream and DataOutputStream in Java

Introduction to DataInputStream and DataOutputStream Classes in Java

DataInputStream and DataOutputStream are two classes in Java that are used to read and write primitive data types from and to a stream, respectively. They are both subclasses of FilterInputStream and FilterOutputStream, respectively. This means that they wrap an existing input or output stream and provide additional functionality for reading and writing primitive data types.

DataInputStream and DataOutputStream are very powerful classes for reading and writing primitive data types from and to a stream. They are used in a wide variety of applications, including networking, database applications, and file processing.

Here are some additional things to keep in mind when using DataInputStream and DataOutputStream:
  • DataInputStream and DataOutputStream are both AutoCloseable classes. This means that they can be automatically closed using the try-with-resources statement.
  • DataInputStream and DataOutputStream can be used to read and write data from and to any stream, not just files.
Methods of DataInputStream Class
Method Description
available() Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking, which may be 0, or 0 when end of stream is detected.
close() Closes this input stream and releases any system resources associated with this stream.
mark(int readlimit) Marks the current position in this input stream. A subsequent reset() will attempt to reposition the stream to this point.
markSupported() Tells whether this input stream supports the mark() and reset() methods.
read() Reads a byte of data from this input stream.
read(byte[] b) Reads up to b.length bytes of data from this input stream into an array of bytes.
read(byte[] b, int off, int len) Reads up to len bytes of data from this input stream into an array of bytes, starting at offset off in the array.
readBoolean() Reads a boolean from the input stream.
readByte() Reads a byte from the input stream.
readChar() Reads a char from the input stream.
readDouble() Reads a double from the input stream.
readFloat() Reads a float from the input stream.
readInt() Reads an int from the input stream.
readLine() Reads a line of text from the input stream.
readLong() Reads a long from the input stream.
readShort() Reads a short from the input stream.
readUnsignedByte() Reads an unsigned byte from the input stream.
readUnsignedShort() Reads an unsigned short from the input stream.
reset() Repositions this stream to the position at which the last mark() was set.
skip(long n) Skips over and discards n bytes of data from this input stream.
Methods of DataOutputStream Class
Method Description
close() Closes this output stream and releases any system resources associated with this stream.
flush() Flushes this output stream and forces any buffered output bytes to be written out to the underlying device.
write() Writes a byte to this output stream.
write(byte[] b) Writes b.length bytes from the specified byte array to this output stream.
write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to this output stream.
writeBoolean(boolean v) Writes a boolean to the output stream.
writeByte(int v) Writes a byte to the output stream.
writeChar(int v) Writes a char to the output stream.
writeChars(String s) Writes the specified string of characters to this output stream.
writeDouble(double v) Writes a double to the output stream.
writeFloat(float v) Writes a float to the output stream.
writeInt(int v) Writes an int to the output stream.
writeLong(long v) Writes a long to the output stream.
writeShort(int v) Writes a short to the output stream.
writeUTF(String str) Writes a string to the output stream using UTF-8 encoding in portable manner.

Exercises

Using DataOutputStream write an Integer to an Output File.


  package practiceproject;
  import java.io.*;
  import java.util.*;
  public class democlass {
  
    public static void main(String[] args) throws IOException {
      
      File obj = new File("satish1.txt");
      FileOutputStream fout = new FileOutputStream(obj);
      DataOutputStream dout = new DataOutputStream(fout);
      dout.write(2);
      dout.writeUTF("satish");
      dout.writeBoolean(true);
      System.out.println("File writing successful");
      fout.close();
      dout.close();
      DataInputStream din = new DataInputStream(new FileInputStream(obj));
      int data=din.read();
      String name = din.readUTF();
      boolean result=din.readBoolean();
      System.out.println(data);
      System.out.println(name);
      System.out.println(result);
    }
  } 

Write and Read an Integer, String and Booolean value using a File. Use dataInputStream and dataOutputStream for the file operations


  package practiceproject;
  import java.io.*;
  import java.util.*;
  public class democlass {
    
      public static void main(String[] args) throws IOException {
        
        File obj = new File("satish1.txt");
        FileOutputStream fout = new FileOutputStream(obj);
        DataOutputStream dout = new DataOutputStream(fout);
        dout.write(2);
        dout.writeUTF("satish");
        dout.writeBoolean(true);
        System.out.println("File writing successful");
        fout.close();
        dout.close();
        DataInputStream din = new DataInputStream(new FileInputStream(obj));
        int data=din.read();
        String name = din.readUTF();
        boolean result=din.readBoolean();
        System.out.println(data);
        System.out.println(name);
        System.out.println(result);
      }
  }