有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java在显示单独进程的输出时打印奇数符号

当我打印我启动的单独进程的输出时,我得到了符号enter image description here。我感兴趣的代码:

in=(char)is.read();
        System.out.println((char)in);
        while (in != -1) {
           outputs[0]=outputs[0]+(char)in;
            in=(char)is.read();
            System.out.println((char)in);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

你知道为什么我的程序会打印这个错误吗?如果我不将输出转换成char形式,我会得到:65535,它不是ASCII符号

谢谢


共 (1) 个答案

  1. # 1 楼答案

    根据InputStream.read()的Javadoc(假设这是is的类型,您不会说):

    The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

    假设你一直把in转换成char,我猜in是一个int;将char分配给int意味着您正在有效地写入in = (int) (char) is.read()(int) (char) -1也是65535