Java ObjectInputStream readUnsignedByte()方法(带示例)
ObjectInputStream类readUnsignedByte()方法
readUnsignedByte()方法在java.io包中可用。
readUnsignedByte()方法用于从ObjectInputStream读取无符号数据字节。
readUnsignedByte()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
readUnsignedByte()方法在读取无符号字节时可能会引发异常。
IOException:在执行过程中遇到任何输入/输出错误时,可能引发此异常。
EOFException:当此流到达末尾时,可能引发此异常。
语法:
public int readUnsignedByte();
参数:
它不接受任何参数。
返回值:
方法的返回类型为int,它返回读取的无符号字节。
示例
//Java程序演示示例
//方法的
//ObjectInputStream-
import java.io.*;
public class ReadUnsignedByteOfOIS {
public static void main(String[] args) throws Exception {
//实例化ObjectOutputStream,ObjectInputStream-
//FileInputStream和FileOutputStream-
FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
ObjectOutputStream obj_out_stm = new ObjectOutputStream(file_out_stm);
FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);
//通过使用writeByte()方法就是写
//字节到obj_out_stm流
obj_out_stm.writeByte(-124);
obj_out_stm.writeByte(131);
obj_out_stm.writeByte(-182);
obj_out_stm.writeByte(154);
obj_out_stm.flush();
while (obj_in_stm.available() > 0) {
//通过使用readUnsingedByte()方法是读取
//obj_in_stm中的无符号字节并返回
//作为整数
int val = (int) obj_in_stm.readUnsignedByte();
System.out.println("obj_in_stm.readUnsignedByte(): " + val);
}
//通过使用close()方法是
//关闭所有流
file_in_stm.close();
file_out_stm.close();
obj_in_stm.close();
obj_out_stm.close();
}
}输出结果
obj_in_stm.readUnsignedByte(): 132 obj_in_stm.readUnsignedByte(): 131 obj_in_stm.readUnsignedByte(): 74 obj_in_stm.readUnsignedByte(): 154