Scanner Vs. Bufferedreader

Scanner vs. BufferedReader

Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing.

In fact you can pass a BufferedReader to a scanner as the source of characters to parse.

which to choose buffered reader or scanner from performance point of view

BufferedReader is a lot faster than Scanner because it buffers the character so you don't have to access the file each time you want to read a char from it.

Scanner are of particular use such as reading primitive data type directly and is also used for regular expressions.

I have used Scanner and BufferedReader both and BufferedReader gives significant fast performance. You can test it yourself too.

BufferedReader vs Console vs Scanner

BufferedReader

  • Since Java 1.1
  • Throws checked exceptions
  • Can read single chars, char arrays, and lines
  • Fast

Scanner

  • Since Java 1.5
  • Throws unchecked exceptions
  • Can read lines, numbers, whitespace-delimited tokens, regex-delimited tokens
  • Difficult to read single characters

Console

  • Since Java 1.6
  • Throws unchecked exceptions
  • Not always available (e.g. if input/output is redirected, and in Eclipse)
  • Can read lines
  • Underlying reader can read single chars and char arrays (but stops at line bounds)
  • Can read passwords (i.e. read without displaying the characters)

Recommendation: Scanner

The methods for reading numbers are very useful (though beware when using nextInt() etc. followed by nextLine()). The exceptions are unchecked, so you do not have to write boilerplate try/catch blocks.

Difference between buffered reader and file reader and scanner class

Well:

  • FileReader is just a Reader which reads a file, using the platform-default encoding (urgh)
  • BufferedReader is a wrapper around another Reader, adding buffering and the ability to read a line at a time
  • Scanner reads from a variety of different sources, but is typically used for interactive input. Personally I find the API of Scanner to be pretty painful and obscure.

To read a text file, I would suggest using a FileInputStream wrapped in an InputStreamReader (so you can specify the encoding) and then wrapped in a BufferedReader for buffering and the ability to read a line at a time.

Alternatively, you could use a third-party library which makes it simpler, such as Guava:

File file = new File("foo.txt");
List<String> lines = Files.readLines(file, Charsets.UTF_8);

Or if you're using Java 7, it's already available for you in java.nio.file.Files:

Path path = FileSystems.getDefault().getPath("foo.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);

Why is Scanner slower than BufferedReader when reading from input?

Upper-level classes/methods are generally slower than lower-level classes/methods.

In the same way you could ask why is searching with regular expressions slower than

searching with String.indexOf(). Actually I've seen such questions here on SO.

The more specialized your class/method is, the better it can perform.

It does e.g. just 1 simple thing but does it quickly and efficiently.

More general classes/methods do e.g. 10-20 different things, so they

are more powerful but due to this they are slower.

I am speaking in general here, I haven't compared Scanner and BufferedReader myself.

what are the benefits of BufferedReader over Scanner

It's an issue of how you intend to use the stream. A buffered reader exists for simple and threaded applications. This is due to scanner's lack of thread safety.

I think you'll get more on this from this question Scanner vs. BufferedReader

BufferedReader vs Scanner, and FileInputStream vs FileReader?

try {
//Simple reading of bytes
FileInputStream fileInputStream = new FileInputStream("path to file");
byte[] arr = new byte[1024];
int actualBytesRead = fileInputStream.read(arr, 0, arr.length);

//Can read characters and lines now
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String lineRead = bufferedReader.readLine();
char [] charArrr = new char[1024];
int actulCharsRead = bufferedReader.read(charArrr, 0, charArrr.length);

//File reader allows reading of characters from a file
FileReader fileReader = new FileReader("path to file");
actulCharsRead = fileReader.read(charArrr, 0, charArrr.length);

//It is a good idea to wrap a bufferedReader around a fileReader
BufferedReader betterFileReader = new BufferedReader(new FileReader(""));
lineRead = betterFileReader.readLine();
actulCharsRead = betterFileReader.read(charArrr, 0, charArrr.length);

//allows reading int, long, short, byte, line etc. Scanner tends to be very slow
Scanner scanner = new Scanner("path to file");
//can also give inputStream as source
scanner = new Scanner(System.in);
long valueRead = scanner.nextLong();

//might wanna check out javadoc for more info

} catch (IOException e) {
e.printStackTrace();
}

Why bufferedReader is much efficient than Scanner class in java?

BufferedReader is significantly faster than Scanner as it has a larger buffer memory than Scanner. You can see the difference between the two in this discussion.



Related Topics



Leave a reply



Submit