Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between BufferedReader and Scanner reading in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the difference between BufferedReader and Scanner reading in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

The java.util.Scanner class is a simple text scanning class that parses basic data types and strings. It essentially uses regular expressions to read different data types.

The Java.io.BufferedReader class reads text from character input streams and character buffers in order to read character sequences efficiently.

In Java, we all know that the standard input string for Java is System.in. But we rarely see who uses it in Java, because we usually enter a string or a number, and so on. The read method provided by System.in reads data through bytes, so it is not convenient for us to deal with it!

Scanner

Java SE6 provides a very convenient class Scanner for entering data, which is located in the java.util package. The specific use of this Scanner is Scanner in = new Scanner (System.in).

To create a Scanner object through new, Scanner needs to pass in a System.in as a parameter, which we can think of as Scanner wrapping System.in through its internal mechanism to read data.

The Scanner object reads the corresponding basic type of data through a series of in.nextXxx (); methods, and determines whether there is the next data through the in.hasNextXxx (); method.

However, Scanner reads the data by splitting the data by the space bar (which includes the space bar, Tab, enter key).

As soon as we encounter one of them, the Scanner method returns the next input (of course, the Terminator of the nextLine () method is a newline character, and it returns the data before the newline character), which is another problem we will face. When there is a space in our input data, we will not get the data we want, so we have to consider BufferReader to read the data!

BufferReader

BufferReader is located in the java.io package, and there are relatively few ways to choose from using BufferReader! Reading data is relatively fixed, so the format is relatively simple, as long as you remember this method of reading data.

BufferedReader br = new BufferedReader (newInputStreamReader (System.in))

The BufferReader object reads data through the readLine (); method. ReadLine () reads a row of data by pressing Enter enter, and everything that precedes the enter key is returned by the readLine () method.

The readLine () method returns a string, so it is relatively troublesome to use BufferReader to enter data of a type other than some characters, which requires some XXXX.parseXxx (); to convert the corresponding data type (for example: int type with Integer.parseInt (string to be converted).

Although troublesome, reading through BufferReader is twice as efficient as Scanner, a gap you can imagine, and the more data you read, the more obvious the effect.

It is important to note that the readLine () method of the BufferedReader object must handle the java.io.IOException exception.

The contrast between the two

1. Scanner provides a series of nextXxx () methods, and it is more convenient to use Scanner when we determine the type of data we enter. It is also because this BufferedReader is faster than Scanner, because Scanner does class parsing of the input data, while BufferedReader simply reads the character sequence.

2. Both Scanner and BufferedReader have buffers, and Scanner has very few buffers (1KB character buffers) relative to BufferedReader (8KB byte buffers), but this is more than sufficient.

3. BufferedReader supports synchronization, but Scanner does not. If we are dealing with multithreaded programs, BufferedReader should use.

4. A problem with Scanner input: in the Scanner class, if we call the nextLine () method after any seven nextXXX () methods, the nextLine () method cannot read anything from the console, and the cursor will not enter the console, it will skip this step. The nextXXX () method includes nextInt (), nextFloat (), nextByte (), nextShort (), nextDouble (), nextLong (), next ().

There is no such problem in the BufferReader class. This problem occurs only in the Scanner class, because the nextXXX () method ignores newline characters, but nextLine () does not ignore it. If we use more than one nextLine () method between the nextXXX () method and the nextLine () method, this problem will not occur; nextLine () consumes the newline character.

Program example package com.zxt.base; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner; public classSystemInTest {private static Scanner sc; public static void main (String [] args) {/ / enter sc = new Scanner (System.in) using Scanner; int num1 = sc.nextInt (); int num2 = sc.nextInt () System.out.println ("num1+ num2 =" + (num1+ num2)); / / enter InputStreamReaderisr = newInputStreamReader (System.in) using BufferReader; BufferedReaderbr = new BufferedReader (isr); try {int num3 = Integer.parseInt (br.readLine ()); int num4 = Integer.parseInt (br.readLine ()); System.out.println ("num3+ num4 =" + (num3+ num4)) } catch (NumberFormatException | IOException e) {e.printStackTrace ();} / / problems encountered when using Scanner input System.out.println (); System.out.print ("Enter an Integer:"); int intValue = sc.nextInt (); System.out.print ("Enter a String:"); StringstrValue = sc.nextLine () System.out.printf ("You have entered intValue is" + intValue+ "and strValue is" + strValue); / / cause of the problem: because the nextXXX () method ignores the newline character, but nextLine () does not ignore it. So if we use nextLine () after the nextXXX () method, there will be a problem / / the solution is: after using the nextXXX () method, before using nextLine () to read the next row of data, use an extra nextLine () to consume the newline character / / int intValue = sc.nextInt (); / / sc.nextLine (); / / String strValue = sc.nextLine () / / or using BufferReader will not cause this problem: System.out.println (); try {System.out.print ("Enter an Integer:"); int intValue1 = Integer.parseInt (br.readLine ()); System.out.print ("Enter a String:"); StringstrValue1 = br.readLine () System.out.printf ("You have entered intValue1 is" + intValue1+ "and strValue1 is" + strValue1);} catch (NumberFormatException | IOException e) {e.printStackTrace ();}}

After reading the above, do you have any further understanding of the difference between BufferedReader and Scanner reading in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report