17.To read a String as input from keyboard in console output using BufferedReader class in java

Published: 09 January 2015
on channel: DASARI TUTS
1,775
10

There are different ways of taking input in java like:

1) BufferedReader 2) Scanner 3) Command Line Arguments

BufferedReader, on the other hand, is a character stream I/O class. Character streams provide a convenient way for input and output in terms of characters (Unicode). BufferedReader is mostly used for taking input from the console, System.in. It takes an InputStreamReader object as an argument. BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

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 Scanner class is the complement of Formater class (used to convert binary data into formatted text). Scanner reads formatted input and converts it into its binary form. Although it has always been possible to read formatted input, it required more effort than most programmers would prefer. Because of the addition of Scanner, it is now easy to read all types of numeric values, strings and other types of data, whether it comes from a disk file, the keyboard, or another source. Scanner can be used to read input from the console, a file, a string, or any other source that implements the Readable interface or ReadableByteChannel. For example, you can use Scanner to read a number from the keyboard and assign its value to a variable.

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.


Watch video 17.To read a String as input from keyboard in console output using BufferedReader class in java online without registration, duration hours minute second in high quality. This video was added by user DASARI TUTS 09 January 2015, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,77 once and liked it 1 people.