从无限循环读取流



我正在为一个小项目尝试用Java进行一些套接字编程。我在从外部进程读取无限输入流时遇到了问题。程序进入无限循环。

我怀疑readLine((必须在EOF之前读取流。

我已经放了一些打印语句,我确定程序到达了while循环。

这是我的方法:

    public void run() throws IOException {
        Process p = Runtime.getRuntime() .exec(exe);
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while((line = in.readLine()) != null){
            System.out.println(line);
            dout.writeUTF(" " + line + " yes!");
            dout.flush();
        }
        in.close();
        dout.close();
        s.close();
        ss.close();
    }

正如@codeflush.dev 提到的,流没有到达EOF,因此readLine不是一个可行的选择。

最新更新