Which is better bufferedreader or scanner




















Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Scanner vs. BufferedReader Ask Question.

Asked 11 years, 9 months ago. Active 3 months ago. Viewed k times. My questions are: Does Scanner perform as well as BufferedReader? Why would you choose Scanner over BufferedReader or vice versa? Ardent Coder 3, 9 9 gold badges 22 22 silver badges 45 45 bronze badges. Not sure if that's actually less efficient, but since reading from std in is blocking, I can't imagine the efficiency of Scanner would be the issue. Add a comment. Active Oldest Votes.

Chandra Sekar Chandra Sekar BufferedReader is synchronized and Scanner is not, so its up to you to decide. I know this topic is old, but I have had mixed results among operating systems using BufferedReader when trying to slurp up content from the streams provided by Process i. Once I changed my code to use Scanner instead, as noted in a separate answer , things started behaving consistently and as expected. Reuben But Scanner depends ultimately on something else for its input, which may well be synchronized.

It's able to give back you specific parts directly as int, string, decimal, etc. See also all those nextXxx methods in Scanner class. It keeps giving back you all characters, which you in turn have to manually inspect if you'd like to match or compose something useful.

But if you don't need to do that anyway, then reading is sufficient. Suragch k gold badges silver badges bronze badges. BalusC BalusC 1. Nice one. Thanks for the buffer tip. Was looking for it all along as native reads are extremely expensive. Now got what is the mean of parsing.

What is I wrap BufferedReader in Scanner's constructor? So if you want a larger buffer, you only need to invoke, e. Hello guys, welcome to my blog. Today, we'll discuss another interesting Java interview question, BufferedReader vs Scanner. It's not only important from an interview point of view but also to work efficiently with Java. Even though both BufferedReader and Scanner can read a file or user input from the command prompt in Java, there are some significant differences between them.

One of the main differences between BufferedReader and Scanner class is that the former class is meant to just read String or text data while the Scanner class is meant to both read and parse text data into Java primitive types like int , short , float , double , and long. In other words, BufferedRedaer can only read String but Scanner can read both String and other data types like int, float, long, double, float, etc.

However, you can share BufferedReader objects between multiple threads. BufferedReader has a much larger memory than the Scanner. Use BufferedReader to get longer strings in the stream, then use Scanner class to analyze some type of token from the stream. The scanner can tokenize using a custom delimiter and parse the stream into primitive data types, while BufferedReader can only read and store String.

A BufferedReader is a class that helps in efficiently reading the underlying stream. Unlike the BufferedReader class, it does the parsing of the input data. Hence it is slow as compared to BufferedReader. To sum up bufferreader vs scanner class in java, both the classes help in taking the input from the command line argument console.

BufferedReader only reads the character-stream data, whereas Scanner has a lot more cheese built into it; it can do all that a BufferedReader can do. The significant difference between them is the buffer size. Irrespective of the buffer size, the scanner does the parsing of the primitive types. Even if you want to read the character-stream input BufferedReader is good for that.

On the other hand, if you want to accept user input with multiple options, Scanner will work best. The disadvantage of the BufferedReader class is that it is hard to remember, whereas the Scanner class is a bit easy. About Us. Privacy Policy. Bug Bounty. Press Release. Career Camp. Captain Coder. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. Writing code in comment? Please use ide. Load Comments. What's New. Most popular in Java.

More related articles in Java.



0コメント

  • 1000 / 1000