-
[JAVA] Scanner와 Buffered Reader의 차이점 (알고리즘)program_language 및 궁금한것/Java 2021. 8. 3. 23:11
알고리즘 공부를 하다보니 java에선 Buffered Reader를 사용하는게 성능향상에 더 좋다고 한다.
검색을 해보니 다양한 이유가 있어서 java api문서와 블로그를 참고해 보았다.
scanner와 BufferedReader는 모두 문자열을 입력받는데 사용되는 클래스이다.
- BufferedReader는 일정한크기(8192chars)의 데이터를 한번에 읽어와 버퍼에 보관 후 사용자 요청시 버퍼에서 데이터를 읽어오는 방식이다. 그래서 시간부하를 줄일 수 있다. 입력받는 모든 형식은 String이다.
- Scanner는 데이터를 입력받는 시점 정규식을 이용해서 테이터 타입이 결정되므로 별도의 Casting이 필요치 않다. 그리고 사용자 요청시 바로바로 데이터를 주기 때문에 데이터 용량이 클 경우 BufferedReader보다 느리다.
그래서 속도는 BufferedReadere가 Scanner보다 빠르다.
BufferedReader Scanner 나온 시점 jdk 1.1 jdk 1.5 buffer size(chars) 8192 1024 동기화 o x 문자열파싱 x o 정규표현식사용 x o https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
Scanner (Java Platform SE 8 )
Scans the next token of the input as a float. This method will throw InputMismatchException if the next token cannot be translated into a valid float value as described below. If the translation is successful, the scanner advances past the input that match
docs.oracle.com
https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html
BufferedReader (Java Platform SE 8 )
Reads characters into a portion of an array. This method implements the general contract of the corresponding read method of the Reader class. As an additional convenience, it attempts to read as many characters as possible by repeatedly invoking the read
docs.oracle.com
반응형'program_language 및 궁금한것 > Java' 카테고리의 다른 글
[java] 박재성님의 리팩토링 후기 (0) 2022.02.09 [JAVA] 특정시간마다 반복 실행 Timer (0) 2022.02.08 [JAVA] 천단위 콤마 넣기 (0) 2021.04.19 [JAVA] 몇 초전 , 몇분전, 몇년전 구하기 (0) 2021.04.15 [ JAVA ] 컴파일에러와 런타임에러의 차이 (0) 2021.03.30