BufferedReader가 Scanner 보다 빠른이유

정태규·2023년 5월 2일
0

java

목록 보기
5/7
post-thumbnail

Oracle java API Documentation을 살펴봤다.

BufferedReader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
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 advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

BufferedReader in
  = new BufferedReader(new FileReader("foo.in"));

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.`

BufferedReader 자체가 문자열,배열,라인등을 효과적으로 읽기 위해 나왔다는 것을 첫줄 부터 알 수 가 있다.
마지막에 나와 있듯이, buffer가 없으면 read()나 readLine()을 호출할때마다, 파일에서 읽어서 문자로 변환하고 리턴해야돼서 매우 비효율적이다.
반면, buffer가 있으면 buffer에 모아두었다가 한번에 전달하기 때문에 훨씬 효율적이다.

0개의 댓글