Java.io.DataInputStream.readByte() Method
Advertisements
Description
The java.io.DataInputStream.readByte() method read and returns one single input byte. The byte is a signed value in the range -128 to 127.
Declaration
Following is the declaration for java.io.DataInputStream.readByte() method:
public final byte readByte()
Parameters
NA
Return Value
The byte value read.
Exception
IOException -- if the stream is closed, or any I/O error occurs.
EOFException -- if the input stream has reached the end.
Example
The following example shows the usage of java.io.DataInputStream.readByte() method.
package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
public static void main(String[] args) throws IOException {
InputStream is = null;
DataInputStream dis = null;
byte[] buf = {65, 0, 0, 68, 69};
try{
// create new byte array input stream
is = new ByteArrayInputStream(buf);
// create data input stream
dis = new DataInputStream(is);
// readBoolean till the data available to read
while( dis.available() >0)
{
// read one single byte
byte b = dis.readByte();
// print the byte
System.out.print(b+" ");
}
}catch(Exception e){
// if any I/O error occurs
e.printStackTrace();
}finally{
// releases any associated system files with this stream
if(is!=null)
is.close();
if(dis!=null)
dis.close();
}
}
}
Let us compile and run the above program, this will produce the following result:
65 0 0 68 69