Home » Java programming language

Java RandomAccessFile readUnsignedByte() Method with Example

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

RandomAccessFile Class readUnsignedByte() method

  • readUnsignedByte() method is available in java.io package.
  • readUnsignedByte() method is used to read unsigned 8-bit byte value from this RandomAccessFile.
  • readUnsignedByte() 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.
  • readUnsignedByte() method may throw an exception at the time of reading unsigned byte 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 1 byte.

Syntax:

    public final int readUnsignedByte();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it returns the next byte (8 bit) of data from this RandomAccessFile manipulated as an unsigned byte integer value.

Example:

// Java program to demonstrate the example 
// of int readUnsignedByte() method of
// RandomAccessFile

import java.io.*;

class RAFReadUnsignedByte {
 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 readUnsignedByte() method is to 
  // read a unsigned byte value up to signed 8 bit
  // from this file

  int by = ra_f.readUnsignedByte();
  System.out.println("ra_f.readUnsignedByte(): " + by);

  // By using close() method isto
  // close this stream ran_f
  ra_f.close();
 }
}

Output

ra_f.readUnsignedByte(): 25


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.