Scanner vs. BufferedReader

前端 未结 12 1742
死守一世寂寞
死守一世寂寞 2020-11-22 02:17

As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner or BufferedReader. I also know that

12条回答
  •  隐瞒了意图╮
    2020-11-22 02:53

    Difference between BufferedReader and Scanner are following:

    1. BufferedReader is synchronized but Scanner is not synchronized.
    2. BufferedReader is thread safe but Scanner is not thread safe.
    3. BufferedReader has larger buffer memory but Scanner has smaller buffer memory.
    4. BufferedReader is faster but Scanner is slower in execution.
    5. Code to read a line from console:

      BufferedReader:

       InputStreamReader isr=new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(isr);
       String st= br.readLine();
      

      Scanner:

      Scanner sc= new Scanner(System.in);
      String st= sc.nextLine();
      

提交回复
热议问题