Last Updated : 05 Jun, 2020
The
readUnsignedByte()method of
DataInputStreamclass in Java is used to read byte and returns as an integer. The integer is an unsigned value in the range from 0 to 255. The bytes in this method are read from the accommodated input stream.
Syntax:public final int readUnsignedByte() throws IOExceptionSpecified By:
This method is specified by readUnsignedByte() method of
DataInputinterface.
Parameters:This method does not accept any parameter.
Return value:This method returns the byte value read as an integer. It is an unsigned 8-bit byte.
Exceptions:Below programs illustrate readUnsignedByte() method in DataInputStream class in IO package:
Program 1: Java
// Java program to illustrate
// DataInputStream readUnsignedByte() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create byte array
byte[] b = { 10, 20, 30, 40, 50 };
// Create byte array input stream
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(b);
// Convert byte array input stream to
// DataInputStream
DataInputStream dataInputStr
= new DataInputStream(
byteArrayInputStr);
while (dataInputStr.available() > 0) {
// Print bytes
System.out.println(
dataInputStr.readUnsignedByte());
}
}
}
Program 2: Java
// Java program to illustrate
// DataInputStream readUnsignedByte() method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create byte array
byte[] b = { -20, -10, 0, 10, 20 };
// Create byte array input stream
ByteArrayInputStream byteArrayInputStr
= new ByteArrayInputStream(b);
// Convert byte array input stream to
// DataInputStream
DataInputStream dataInputStr
= new DataInputStream(
byteArrayInputStr);
while (dataInputStr.available() > 0) {
// Print bytes
System.out.println(
dataInputStr.readUnsignedByte());
}
}
}
References: https://docs.oracle.com/javase/10/docs/api/java/io/DataInputStream.html#readUnsignedByte()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4