Java IO Tutorial - Java DataInputStream .readUnsignedByte ()








Syntax

DataInputStream.readUnsignedByte() has the following syntax.

public final int readUnsignedByte()    throws IOException

Example

In the following code shows how to use DataInputStream.readUnsignedByte() method.

/*from  w  w  w . j  a  va 2  s  .c o m*/

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
  public static void main(String[] args) throws IOException {

    byte[] b = { -123, 4 };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (byte j : b) {
      dos.writeByte(j);
    }
    dos.flush();
    InputStream is = new FileInputStream("c:\\test.txt");
    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
      int k = dis.readUnsignedByte();
      System.out.print(k);
    }

  }
}