Java IOException - Stream Closed

前端 未结 2 1406
心在旅途
心在旅途 2021-01-19 12:12

I get \"IOException: Stream Closed\" when I run this program. The text contains many lines of data. Program should read each line, do necessary functio

相关标签:
2条回答
  • 2021-01-19 12:59

    You close the input stream in your loop:

    while ((inputLine = in.readLine()) != null) // error
    
                   // System.out.println(inputLine);
    in.close();  
    

    You should close the stream outside of the loop:

    while ((inputLine = in.readLine()) != null) // error
    {
       //dosomething
       // System.out.println(inputLine);
    }
    in.close();  
    
    0 讨论(0)
  • 2021-01-19 13:06

    You should put a function call in the while loop, like:

    1. a System.out.println("Hi, I'm a row!"); or
    2. uncomment System.out.println(inputLine); or
    3. put a semicolon at the end of the while statement

    in order to let it to execute properly.

    The code as it is written executes (comments omitted):

    ...
       while ((inputLine = in.readLine()) != null)
         in.close();  
    ...
    

    so the first cycle of the loop executes correctly and runs in.close(). Then the second cycle the call inputLine = in.readLine() fails because the stream is closed and then the exception is thrown.

    0 讨论(0)
提交回复
热议问题