Search code examples
javajava.util.scannerbufferedreaderbufferedinputstream

Performance measure of BufferedReader Vs BufferedInputStream


Recently I have modified my code to

  • While taking input form STDIN, I moved from Scanner to BufferedInputStream.
  • I also read about BufferedReader which takes the input from any InputStreamReader. This InputStreamReader can be used with System.in to take STDIN input.
  • BufferedInputStream has read() method, which further needs to be parsed according to the objective.

In my case first i need to take an integer (let say n) as input from STDIN after that a for loop will take n strings as input. These strings have at max 1,00,000 characters.

Question is : Which one among Scanner, BufferedInputStream and BufferedReader performs better for my objective?


Solution

  • Scanner was designed to simplify acceptance of input parameters at run time from user. This is the java equivalent of scanf()/getc()/cin. The 'Reader's are used to read character data, 'Stream's for streamed data. Scanner is best suited for your purpose. As it is simple to code and use.