Home » Java programming language

Java DataInputStream readUnsignedShort() Method with Example

DataInputStream Class readUnsignedShort() method: Here, we are going to learn about the readUnsignedShort() method of DataInputStream Class with its syntax and example.
Submitted by Preeti Jain, on March 30, 2020

DataInputStream Class readUnsignedShort() method

  • readUnsignedShort() method is available in java.io package.
  • readUnsignedShort() method is used to read 2 bytes (i.e. 16 bit) of data input and returns an unsigned integer value read.
  • readUnsignedShort() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • readUnsignedShort() method may throw an exception at the time of reading unsigned short.
    • IOException: This exception may throw when this stream is not opened.
    • EndOfFileException: This exception may throw when this stream has reached its endpoint.

Syntax:

    public final int readUnsignedShort();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is int, it returns 2 bytes of data input, manipulated as an unsigned integer value.

Example:

// Java program to demonstrate the example 
// of int readUnsignedShort() method of 
// DataInputStream

import java.io.*;

public class ReadUnsignedShortOfDIS {
 public static void main(String[] args) throws IOException {
  InputStream is_stm = null;
  DataInputStream dis_stm = null;
  FileOutputStream fos_stm = null;
  DataOutputStream dos_stm = null;
  short[] s_arr = {
   125,
   -8,
   327,
   -124
  };

  try {
   // Instantiate FileInputStream, 
   // DataInputStream, FileOutputStream
   // and DataOutputStream 

   fos_stm = new FileOutputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
   dos_stm = new DataOutputStream(fos_stm);

   // Loop to write each short till end
   for (short val: s_arr) {
    // By using writeShort() method isto
    // write a short value to the
    // DataOutputStream dos_stm
    dos_stm.writeShort(val);
   }

   is_stm = new FileInputStream("C:\\Users\\Preeti Jain\\Desktop\\programs\\includehelp.txt");
   dis_stm = new DataInputStream(is_stm);

   // Loop To Read Available Data till end
   while (dis_stm.available() > 0) {
    // By using readUnsignedShort() method isto read 
    // unsigned short at a time from dis_stm
    int in = dis_stm.readUnsignedShort();
    System.out.println("dis_stm.readUnsignedShort(): " + in );
   }
  } catch (Exception ex) {
   System.out.println(ex.toString());
  } finally {
   // To free system resources linked
   // with these streams
   if (is_stm != null)
    is_stm.close();

   if (dis_stm != null)
    dis_stm.close();

   if (dos_stm != null)
    dos_stm.close();

   if (fos_stm != null)
    fos_stm.close();
  }
 }
}

Output

dis_stm.readUnsignedShort(): 125
dis_stm.readUnsignedShort(): 65528
dis_stm.readUnsignedShort(): 327
dis_stm.readUnsignedShort(): 65412






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.