“java.io.IOException: Stream closed” with new BufferedReader

前端 未结 2 1640
醉话见心
醉话见心 2021-01-29 05:40

Many people asked question like that but this one is a little bit different. Here is the code:

public static BufferedReader reader;    
public static String read         


        
相关标签:
2条回答
  • 2021-01-29 05:57

    Closing the BufferedReader closes System.in. You shouldn't close it at all, and you shouldn't keep creating a new one either: you will lose data. Use the same one for the life of the process.

    0 讨论(0)
  • 2021-01-29 06:05

    Because System.in is same Object (public final static InputStream in; of System class) both method calls are using, closing in one method will automatically close System.in for other method. You should close the BufferedReader from outside(as I can see it's public) the method once you finish calling readString and so it will ultimately close underlying System.in.

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