java.util.Scanner
類是一個簡單的文本掃描類,它可以解析基本數(shù)據(jù)類型和字符串。它本質(zhì)上是使用正則表達(dá)式去讀取不同的數(shù)據(jù)類型。
Java.io.BufferedReader
類為了能夠高效的讀取字符序列,從字符輸入流和字符緩沖區(qū)讀取文本。
在Java中,我們都知道Java的標(biāo)準(zhǔn)輸入串是System.in。但是我們卻很少在Java中看到誰使用它,這是因為我們平時輸入的都是一個字符串或者是一個數(shù)字等等。而System.in提供的read方法是通過字節(jié)來讀取數(shù)據(jù)的,所以對我們來說不方便處理!
Scanner
在Java SE6提供了一個非常方便的輸入數(shù)據(jù)的類Scanner,位于java.util包中,這個Scanner的具體用法為Scanner in = new Scanner(System.in)。
通過new創(chuàng)建一個Scanner對象,Scanner需要傳入一個System.in作為參數(shù),這個我們可以看作是Scanner通過其內(nèi)部機(jī)制將System.in包裝起來而實現(xiàn)數(shù)據(jù)的讀取工作的。
Scanner對象通過一系列的in.nextXxx();方法來讀取相應(yīng)的基本類型的數(shù)據(jù),通過in.hasNextXxx();方法來判斷是否還有下一個數(shù)據(jù)。
然而,Scanner讀取數(shù)據(jù)是按空格符(這其中包括空格鍵,Tab鍵,Enter鍵)來分割數(shù)據(jù)的。
只要遇到其中之一,Scanner的方法就會返回下一個輸入(當(dāng)然nextLine()方法的結(jié)束符為換行符,它會返回?fù)Q行符之前的數(shù)據(jù)),這也就是我們會面臨的另一個問題,當(dāng)我們的輸入數(shù)據(jù)中有空格時,我們就不會得到我們想要的數(shù)據(jù),這樣我們就要考慮到BufferReader來讀取數(shù)據(jù)!
BufferReader
BufferReader位于java.io包中,使用BufferReader就相對來說沒有那么多方法來讓你選擇!讀取數(shù)據(jù)比較固定,這樣格式也就相對來說比較單一,只要記住就這一讀取數(shù)據(jù)的方法。
1
|
BufferedReader br = new BufferedReader (newInputStreamReader(System.in)); |
這個BufferReader對象通過readLine();方法來讀取數(shù)據(jù),readLine()是按Enter回車來讀取一行數(shù)據(jù)的,只要在回車鍵之前的都會被readLine()方法返回。
readLine()方法返回的是字符串,因此要使用BufferReader輸入一些字符之外的類型的數(shù)據(jù),就要相對比較麻煩,需要通過一些XXXX.parseXxx();來轉(zhuǎn)換相應(yīng)的數(shù)據(jù)類型,(例如:int類型的用Integer.parseInt(需要轉(zhuǎn)換的字符串))。
雖然麻煩一些,但通過BufferReader讀入的效率要比Scanner高一倍,這個差距可想而知,而且讀取的數(shù)據(jù)越多,效果就越明顯。
需要注意的是使用BufferedReader對象的readLine()方法必須處理java.io.IOException異常。
兩者的對比
1、Scanner提供了一系列nextXxx()方法,當(dāng)我們確定輸入的數(shù)據(jù)類型時,使用Scanner更加方便。也正是因為這個BufferedReader相對于Scanner來說要快一點,因為Scanner對輸入數(shù)據(jù)進(jìn)行類解析,而BufferedReader只是簡單地讀取字符序列。
2、Scanner和BufferedReader都設(shè)置了緩沖區(qū),Scanner有很少的緩沖區(qū)(1KB字符緩沖)相對于BufferedReader(8KB字節(jié)緩沖),但是這是綽綽有余的。
3、BufferedReader是支持同步的,而Scanner不支持。如果我們處理多線程程序,BufferedReader應(yīng)當(dāng)使用。
4、Scanner輸入的一個問題:在Scanner類中如果我們在任何7個nextXXX()方法之后調(diào)用nextLine()方法,這nextLine()方法不能夠從控制臺讀取任何內(nèi)容,并且,這游標(biāo)不會進(jìn)入控制臺,它將跳過這一步。nextXXX()方法包括nextInt(),nextFloat(), nextByte(),nextShort(),nextDouble(),nextLong(),next()。
在BufferReader類中就沒有那種問題。這種問題僅僅出現(xiàn)在Scanner類中,由于nextXXX()方法忽略換行符,但是nextLine()并不忽略它。如果我們在nextXXX()方法和nextLine()方法之間使用超過一個以上的nextLine()方法,這個問題將不會出現(xiàn)了;因為nextLine()把換行符消耗了。
程序示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
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) { // 使用Scanner輸入 sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); System.out.println( "num1 + num2 = " + (num1+ num2)); // 使用BufferReader輸入 InputStreamReaderisr = newInputStreamReader(System.in); 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(); } // 使用Scanner輸入會遇到的問題 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); // 問題原因:由于nextXXX()方法忽略換行符,但是nextLine()并不忽略它。所以如果我們在nextXXX()方法后面使用nextLine()將會出現(xiàn)問題 // 解決方案是:在使用nextXXX()方法后,在使用nextLine()讀取下一行數(shù)據(jù)前,多使用一個nextLine()用來消耗換行符 // int intValue = sc.nextInt(); // sc.nextLine(); // String strValue = sc.nextLine(); // 或者使用BufferReader不會出現(xiàn)該問題 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(); } } } |
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/zengxiantao1994/article/details/78056243