BufferedReader冻结无异常求助:卡在readline()执行行
readline() Without Exception Let's break down why your code is freezing at line = processOutput.readline() after reading 19 lines, and walk through actionable fixes:
Why This Happens
The readline() method is blocking by design: it waits indefinitely for either a new line of input or the end of the stream (which returns null). If your program freezes after line 19, it almost always means one of two things:
- The child process is still running but hasn't produced any new output to stdout
- Or the child process is stuck (e.g., waiting for user input, or its error stream buffer is full and it can't proceed)
Since you mentioned the freeze happens at the "output end" instead of mid-line, it's likely the child process hasn't closed its stdout stream yet, and readline() is waiting for more data that never arrives.
Fixes to Try
1. Handle the Child Process's Error Stream (Critical!)
Most developers forget that child processes write error messages to stderr, not stdout. If the stderr buffer fills up, the child process will hang entirely—which in turn causes your readline() on stdout to freeze. You need to read both streams simultaneously.
Use a separate thread to handle stderr while you read stdout:
// Spin up a thread to read and log errors new Thread(() -> { try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) { String errorLine; while ((errorLine = errorReader.readLine()) != null) { System.err.println("Child process error: " + errorLine); } } catch (IOException e) { e.printStackTrace(); } }).start(); // Read stdout as you were before try (BufferedReader processOutput = new BufferedReader(new InputStreamReader(process.getInputStream()))) { String line; while ((line = processOutput.readLine()) != null) { System.out.println(line); } }
2. Check if the Child Process is Waiting for Input
If the child process expects interactive input (e.g., prompting for a value), it will pause execution until it gets that input. This stops it from writing to stdout, leaving your readline() hanging.
- If you don't need to send input to the child, close its input stream to signal no more input is coming:
process.getOutputStream().close(); - If you do need to send input, write to the output stream and flush it immediately:
try (PrintWriter writer = new PrintWriter(process.getOutputStream())) { writer.println("required input value"); writer.flush(); // Ensure the input is sent right away }
3. Force the Child Process to Flush Output
If the child process is code you control, make sure it flushes the output stream after writing—especially if it doesn't end lines with \n. Buffered streams hold data until they're full or flushed, so the parent process won't see the data until then:
// Inside the child process code System.out.println("output line content"); System.out.flush(); // Push data to the parent immediately
4. Use Non-Blocking Checks or Timeouts
If you want to avoid indefinite blocking, you can check if there's data available before calling readline():
// Wait for the process to finish while checking for output while (process.isAlive()) { if (processOutput.ready()) { String line = processOutput.readLine(); if (line != null) { System.out.println(line); } } else { // Pause briefly to avoid spamming the CPU Thread.sleep(100); } } // Read any remaining output after the process exits String leftoverLine; while ((leftoverLine = processOutput.readLine()) != null) { System.out.println(leftoverLine); }
Quick Debug Tip
To confirm if the child process is still running, add a log line that checks process.isAlive() right before the freezing readline() call. If it returns true, the process is still active and either stuck or waiting to produce more output.
内容的提问来源于stack exchange,提问作者Chawn Neal

