Should I buffer the InputStream or the InputStreamReader?

后端 未结 4 2101

What are the differences (if any) between the following two buffering approaches?

Reader r1 = new BufferedReader(new InputStreamReader(in, \"UTF-8\"), bufferSize         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 03:57

    r1 is also more convenient when you read line-based stream as BufferedReader supports readLine method. You don't have to read content into a char array buffer or chars one by one. However, you have to cast r1 to BufferedReader or use that type explicitly for the variable.

    I often use this code snippet:

    BufferedReader br = ...
    String line;
    while((line=br.readLine())!=null) {
      //process line
    }
    

提交回复
热议问题