What is the difference between Java's BufferedReader and InputStreamReader classes?

前端 未结 7 1887
我在风中等你
我在风中等你 2020-12-02 09:19

What is the difference between Java\'s BufferedReader and InputStreamReader classes?

相关标签:
7条回答
  • 2020-12-02 09:58

    BufferedReader is a class in Java that reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, lines and arrays. The buffer size may be specified. If not, the default size, which is predefined, may be used.

    In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore good practice to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

    FileReader reader = new FileReader(“MyFile.txt”);
    BufferedReader bufferedReader = new BufferedReader(reader);
    

    will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.

    Source: https://medium.com/@isaacjumba/why-use-bufferedreader-and-bufferedwriter-classses-in-java-39074ee1a966

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