Open In App

Java.io.DataInputStream class in Java | Set 2

Improve
Improve
Like Article
Like
Save
Share
Report

Java.io.DataInputStream class in Java | Set 1

More Methods:

  • byte readByte() : Reads and returns one input byte.
    Syntax:public final byte readByte()
                        throws IOException
    Returns: the next byte of this input stream as a signed 8-bit byte.
    Throws:
    EOFException
    IOException 
    
  • float readFloat() : Reads four input bytes and returns a float value.
    Syntax:public final float readFloat()
                          throws IOException
    Returns :the next four bytes of this input stream, interpreted as a float.
    Throws:
    EOFException 
    IOException
  • void readFully(byte[] b) : Reads some bytes from an input stream and stores them into the buffer array b.
    Syntax:public final void readFully(byte[] b)
                         throws IOException
    Parameters:
    b - the buffer into which the data is read.
    Throws:
    EOFException
    IOException
  • void readFully(byte[] b, int off, int len) : Reads len bytes from an input stream.
    Syntax:public final void readFully(byte[] b,
                 int off,
                 int len)
                         throws IOException
    Parameters:
    b - the buffer into which the data is read.
    off - the start offset of the data.
    len - the number of bytes to read.
    Throws:
    EOFException 
    IOException
  • String readLine() : Reads the next line of text from the input stream.
    Syntax:
    Returns: the next line of text from this input stream.
    Throws:
    IOException
    Deprecated This method does not properly convert bytes to characters.
    
  • long readLong() : Reads eight input bytes and returns a long value.
    Syntax:public final long readLong()
                        throws IOException
    Returns:
    the next eight bytes of this input stream, interpreted as a long.
    Throws:
    EOFException
    IOException
  • short readShort() : Reads two input bytes and returns a short value.
    Syntax:public final short readShort()
                          throws IOException
    Returns:
    the next two bytes of this input stream, interpreted as a signed 16-bit number.
    Throws:
    EOFException
    IOException
  • String readUTF() : Reads  a string that has been encoded using a modified UTF-8 format.
    Syntax:public final String readUTF()
                         throws IOException
    Returns:
    a Unicode string.
    Throws:
    EOFException 
    IOException
    UTFDataFormatException 
    
  • int skipBytes(int n) : Makes an attempt to skip over n bytes of data from the input stream, discarding the skipped bytes.
    Syntax:public final int skipBytes(int n)
                        throws IOException
    Parameters:
    n - the number of bytes to be skipped.
    Returns:
    the actual number of bytes skipped.
    Throws:
    IOException

Program 2:




//Java program to demonstrate DataInputStream
// This program uses try-with-resources. It requires JDK 7 or later.
import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
  
class DataInputStreamDemo
{
    public static void main(String args[]) throws IOException
    {
        //writing the data.
        try ( DataOutputStream dout =
                    new DataOutputStream(new FileOutputStream("file.dat")) )
        {
            dout.writeBytes("1");
            dout.writeFloat(4.4545f);
            dout.writeUTF("geeksforgeeks");
            dout.writeChars("GeeksforGeeks\n");
            dout.writeBytes("ABCDEFG");
  
        }
  
        catch(FileNotFoundException ex)
        {
            System.out.println("Cannot Open the Output File");
            return;
        }
  
        // reading the data back.
        try ( DataInputStream din =
                    new DataInputStream(new FileInputStream("file.dat")) )
        {
  
            //illustrating readByte() method
            byte t=din.readByte();
              
            //illustrating readFloat() method
            float u=din.readFloat();
              
            //illustrating readUTF() method
            String temp=din.readUTF();
              
            //illustrating readLine() method
            String temp1=din.readLine();
  
            System.out.println("Values: " + t +" "+" "+u+" "+temp+" "+temp1 );
  
            //illustrating skipBytes() method
            //skipping "AB"
            din.skipBytes(2);
  
            //illustrating readFully() method
            byte[] b=new byte[4];
            din.readFully(b,0,4);
            System.out.println(Arrays.toString(b));
  
        }
        catch(FileNotFoundException e)
        {
            System.out.println("Cannot Open the Input File");
            return;
        }
    }
}


Output:

Values: 49  4.4545 geeksforgeeks  GeeksforGeeks 
[67, 68, 69, 70]


Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads