Home »
Java programming language
Java RandomAccessFile readUnsignedShort() Method with Example
RandomAccessFile Class readUnsignedShort() method: Here, we are going to learn about the readUnsignedShort() method of RandomAccessFile Class with its syntax and example.
Submitted by Preeti Jain, on March 23, 2020
RandomAccessFile Class readUnsignedShort() method
- readUnsignedShort() method is available in java.io package.
- readUnsignedShort() method is used to read unsigned 16-bit short value from this RandomAccessFile.
- 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 integer value.
- IOException: This exception may throw an exception while performing input/output operation.
- EOFException: This exception may throw when the file pointer reaches EOF (End-Of-File) before reading 2 bytes.
Syntax:
public final int readUnsignedShort();
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is int, it returns the next 2 bytes (16 bit) of data from this RandomAccessFile manipulated as an unsigned short integer value.
Example:
// Java program to demonstrate the example
// of int readUnsignedShort() method of
// RandomAccessFile
import java.io.*;
class RAFReadUnsignedShort {
public static void main(String[] args) throws Exception {
// Instantiate a random access file
// object with file name and permissions
RandomAccessFile ra_f = new RandomAccessFile("e:/includehelp.txt", "rw");
// By using writeUTF() method is to
// write data to the file
ra_f.writeUTF("Welcome, in Java World!!!");
// Initially set the file pointer
// is at 1 for reading the file
ra_f.seek(1);
// By using readUnsignedShort() method is to
// read a unsigned short value up to signed 16 bit
// from this file
int sh = ra_f.readUnsignedShort();
System.out.println("ra_f.readUnsignedShort(): " + sh);
// By using close() method isto
// close this stream ran_f
ra_f.close();
}
}
Output
ra_f.readUnsignedShort(): 6487