Wednesday, July 3, 2024
HomeLanguagesJavaDataInputStream readUnsignedShort() method in Java with Examples

DataInputStream readUnsignedShort() method in Java with Examples

The readUnsignedShort() method of DataInputStream class in Java is used to read two input bytes and returns an integer value. This method reads the next two bytes from the input stream and interprets it into integer type and returns.

Syntax:

public final int readUnsignedShort()
                 throws IOException

Specified By: This method is specified by the readUnsignedShort() method of DataInput interface.

Parameters: This method does not accept any parameter.

Return value: This method returns the integer value interpreted by the next two bytes of the input stream.

Exceptions:

  • EOFException – It throws EOFException if the input stream is ended before two bytes can be read.
  • IOException – This method throws IOException if the stream is closed or some other I/O error occurs.

Below programs illustrate readUnsignedShort() method in DataInputStream class in IO package:

Program 1: Assume the existence of file “demo.txt”.




// Java program to illustrate
// DataInputStream readUnsignedShort() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create short array
        short[] buf = { 10, 20, 30, 40, 50 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (short b : buf) {
            // Write short value to
            // the dataOutputStream
            dataOutputStr.writeShort(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print short values
            System.out.println(
                dataInputStr.readSignedShort());
        }
    }
}


Output:

Program 2: Assume the existence of file “demo.txt”.




// Java program to illustrate
// DataInputStream readUnsignedShort() method
import java.io.*;
public class GFG {
    public static void main(String[] args)
        throws IOException
    {
  
        // Create short array
        short[] buf = { 191, 225, 480, 763, 500 };
  
        // Create file output stream
        FileOutputStream outputStream
            = new FileOutputStream("c:\\demo.txt");
  
        // Create data output stream
        DataOutputStream dataOutputStr
            = new DataOutputStream(outputStream);
  
        for (short b : buf) {
            // Write short value to
            // the dataOutputStream
            dataOutputStr.writeShort(b);
        }
  
        dataOutputStr.flush();
  
        // Create file input stream
        FileInputStream inputStream
            = new FileInputStream("c:\\demo.txt");
  
        // Create data input stream
        DataInputStream dataInputStr
            = new DataInputStream(inputStream);
  
        while (dataInputStr.available() > 0) {
            // Print short values
            System.out.println(
                dataInputStr.readUnsignedShort());
        }
    }
}


Output:

References:
https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readUnsignedShort()

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments