很简单的问题,但是说我想使用while循环来等待输入到我的ObjectInputStream,如下所示:

while ((messageObject = objectInputStream.readObject()) != null) {
    // Do something once object is passed through inputstream
}


我如何使用ObjectInputStream的readBoolean方法实现此功能而又不将布尔值转换为其他值呢?

例如:

while ((messageBoolean = objectInputStream.readBoolean()) != null) {
    // Do something once boolean is passed through inputstream
}


上面的代码仅给出了编译时错误:不可比较的类型:布尔值和

编辑:我应该澄清:

objectInputStream = ObjectInputStream(socket.getInputStream())

最佳答案

您可以执行以下操作:

boolean valueRead=false;
boolean endOfFile=false;
while(!endOfFile) {
    try {
        valueRead = objectInputStream.readBoolean();
        //Do something with the boolean
    } catch (EOFException e) {
        endOfFile = true;
    }
}


看看readBoolean

关于java - Java-while循环内的ObjectInputStream boolean 分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33834999/

10-09 02:58