Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > Какая разница между Scanner и BufferedReader?


Автор: akvarel 26.8.2011, 21:38
Я нигде не могу найти лёгких(я начинающий) и разъясняющих разницу примеров.

Вот что я вычитала и поняла:
Scanner разделяет текст на отрывки слов , распознавая пробел. Он имеет так же методы nextInt, nextDouble... ,чтобы позволяет пропускать ненужные символы. Сканнер не синхронихирован-небезопасный.Он так же не имеет exeptions, если,например, файл не найден.

BufferedReader читает длиные файлы быстрее, синхронизирован, имеет exeption, и с ним можно читать каждую букву в отдельности.

Но при каких обстоятельствах вы будете использовать Scanner или BufferedReader? 

Спасибо

Автор: priam220 28.8.2011, 00:14
Отличный http://en.allexperts.com/q/Java-1046/2009/2/Difference-Scanner-Method-Buffered.htm:
Цитата

  A BufferedReader is a simple class meant to efficiently read from the underling stream. Generally, each read request made of a Reader like a FileReader causes a corresponding read request to be made to underlying stream. 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. Efficiency is improved appreciably if a Reader is warped in a BufferedReader.
   BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads.

   A scanner on the other hand has a lot more cheese built into it; it can do all that a BufferedReader can do and at the same level of efficiency as well. However, in addition a Scanner can parse the underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice. It can also do forward scanning of the underlying stream disregarding the delimiter!
   A scanner however is not thread safe, it has to be externally synchronized.

  The choice of using a BufferedReader or a Scanner depends on the code you are writing, if you are writing a simple log reader Buffered reader is adequate. However if you are writing an XML parser Scanner is the more natural choice. 

  Even while reading the input, if want to accept user input line by line and say just add it to a file, a BufferedReader is good enough. On the other hand if you want to accept user input as a command with multiple options, and then intend to perform different operations based on the command and options specified, a Scanner will suit better.  

 

Автор: aleksandy 29.8.2011, 07:08
Цитата(akvarel @  26.8.2011,  21:38 Найти цитируемый пост)
Но при каких обстоятельствах вы будете использовать Scanner или BufferedReader? 

http://forum.vingrad.ru/index.php?showtopic=326859&view=findpost&p=2332860

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)