Java DataInputStream readBoolean()方法(带示例)
DataInputStream类readBoolean()方法
readBoolean()方法在java.io包中可用。
readBoolean()方法用于检查此流是否读取布尔值。
readBoolean()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
readBoolean()方法在读取布尔值时可能会引发异常。
IOException:如果未打开此流,则可能引发此异常。
EndOfFileException:当此流到达其端点时,可能引发此异常。
语法:
public final boolean readBoolean();
参数:
它不接受任何参数。
返回值:
该方法的返回类型为boolean,当byte为非零时返回true,否则返回false。
示例
//Java程序演示示例
//方法
//DataInputStream-
import java.io.*;
public class ReadBooleanOfDIS {
public static void main(String[] args) throws Exception {
InputStream is_stm = null;
DataInputStream dis_stm = null;
byte[] b_arr = {
97,
0,
99,
100,
0,
101
};
try {
//实例化ByteArrayInputStream和
//DataInputStream-
is_stm = new ByteArrayInputStream(b_arr);
dis_stm = new DataInputStream(is_stm);
//循环读取可用数据直到结束
while (dis_stm.available() > 0) {
//通过使用readBoolean()方法返回true-
//如果读取的字节不为零,否则
//它返回false-
boolean status = dis_stm.readBoolean();
System.out.println("dis_stm.readBoolean(): " + status);
}
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
//要释放链接的系统资源
//这些流
if (is_stm != null)
is_stm.close();
if (dis_stm != null)
dis_stm.close();
}
}
}输出结果
dis_stm.readBoolean(): true dis_stm.readBoolean(): false dis_stm.readBoolean(): true dis_stm.readBoolean(): true dis_stm.readBoolean(): false dis_stm.readBoolean(): true