本文為轉載學習使用,用於自己學習試試,原文來自:https://blog.csdn.net/qq_36631076/article/details/77006007
四種都是Java中獲取鍵盤輸入值的方法
1 System.in
System.in返回的是InputStream指向命令行輸入的字節流,它的read方法以字節流的方式來讀取命令行的輸入的數據。
查看源碼我們常用的有:
int System.read() //以字節的方式讀取輸入的第一字符,返回該字符的ASCII碼
int System.read(byte b[]) //以字節的方式把輸入的字符放入byte數組中
int System.read(byte b[], int off, int len) //以字節的方式把輸入的字符放入byte數組中 off是起始位置,len是最大讀入的字節數。
使用實例:
int num = System.in.read(); // 輸入 abc
System.out.println(num); // 輸出 97 PS:97是a的ASCII碼值
// 定義一個byte數組
byte[] b = new byte[10]; // 輸入 " abc "(不算上"" 這個只是為了看清空格鍵,意思是,把空格鍵也計算上去了,截圖是不算上或回車鍵,只有換行)
int len = System.in.read(b);
System.out.println(len); // 輸出 5 ,這里加上了回車鍵和換行鍵
System.out.println(Arrays.toString(b));
// 輸出[97, 98, 99, 13, 10, 0, 0, 0, 0, 0]
// 這里的97 98 99 分別是a、b、c的ASCII碼值,13,10分別是 回車鍵 和 換行鍵 的ASCII碼值
2 Scanner
java.util.Scanner是Java5的新特征,主要功能是簡化文本掃描,這個類最實用的地方表現在獲取控制台輸入。當通過new Scanner(System.in)創建一個Scanner,控制台會一直等待輸入,直到敲回車鍵結束,把所輸入的內容傳給Scanner,作為掃描對象。如果要獲取輸入的內容,則只需要調用Scanner的nextLine()方法即可。
Scanner也可以從字符串(Readable)、輸入流、文件等等來直接構建Scanner對象,有了Scanner了,就可以逐段(根據正則分隔式)來掃描整個文本,並對掃描后的結果做想要的處理。
控制台掃描:
Scanner input = new Scanner(System.in);
while (true) {
String line = sc.nextLine();
if (line.equals("exit")) {
break; //如果輸入為"exit",則退出
}
System.out.println("輸入:" + line);
}
- Scanner默認使用空格作為分割符來分隔文本,但允許你指定新的分隔符:
// 默認以空格方式分割文本
Scanner sc = new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf ......asdfkl las");
// 以自己制定方式分割文本,支持正則表達式
//sc.useDelimiter(" |,|\\.");
while (sc.hasNext()) {
System.out.println(sc.next());
}
重要滴API
// 默認以空格方式分割文本
Scanner input= new Scanner("123 asdf sd 45 789 sdf asdfl,sdf.sdfl,asdf ......asdfkl las");
// 以自己制定方式分割文本,支持正則表達式
//input.useDelimiter(" |,|\\.");
while (input.hasNext()) {
System.out.println(input.next());
}
3 InputStreamReader
InputStreamReader是字節流通向字符流的橋梁:它使用指定的 charset 讀取字節並將其解碼為字符。它使用的字符集可以由名稱指定或顯式給定,或者可以接受平台默認的字符集
InputStreamReader (InputStream in) // 創建一個使用默認字符集的 InputStreamReader。
InputStreamReader (InputStream in, Charset cs) // 創建使用給定字符集的 InputStreamReader。
InputStreamReader (InputStream in, CharsetDecoder dec) // 創建使用給定字符集解碼器的 InputStreamReader。
InputStreamReader (InputStream in, String charsetName) // 創建使用指定字符集的 InputStreamReader。
InputStreamReader重要API
public int read() //以字節的方式讀取輸入的第一字符,返回該字符的ASCII碼
public int read(char cbuf[]) //以字節的方式把輸入的字符放入char數組中
public int read(char cbuf[], int offset, int length) //以字節的方式把輸入的字符放入char數組中 offset是起始位,length是最大讀入的字節數。
每次調用 InputStreamReader 中的一個 read() 方法都會導致從底層輸入流讀取一個或多個字節。要啟用從字節到字符的有效轉換,可以提前從底層流讀取更多的字節,使其超過滿足當前讀取操作所需的字節。 為了達到最高效率,可要考慮在 BufferedReader 內包裝 InputStreamReader。
例如:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //用InputStreamReader來構造BufferedReader
InputStreamReader最大的特點是可以指轉換的定編碼格式,這是其他類所不能的,從構造方法就可看出,這一點在讀取中文字符時非常有用
4 BufferesReader
BufferedReader 從字符輸入流中讀取文本,緩沖各個字符,從而實現字符、數組和行的高效讀取。(通常和InputStreamReader連用,因為輸入的數據是字節流,需要InputStreamReader將其轉成成字符流)
private static int defaultCharBufferSize = 8192; //文本緩存大小的默認值
public BufferedReader(Reader in) {
this(in, defaultCharBufferSize);
}
public BufferedReader(Reader in, int sz)
BufferedReader重要API-取自源碼:
/**
* Reads a line of text. A line is considered to be terminated by any one
* of a line feed ('\n'), a carriage return ('\r'), or a carriage return
* followed immediately by a linefeed.
*
* @param ignoreLF If true, the next '\n' will be skipped
*
* @return A String containing the contents of the line, not including
* any line-termination characters, or null if the end of the
* stream has been reached
*
* @see java.io.LineNumberReader#readLine()
*
* @exception IOException If an I/O error occurs
*/
String readLine(boolean ignoreLF) throws IOException // 這個是源碼上的注釋
public String readLine() throws IOException { //這里默認ignoreLF為false
return readLine(false);
}
public int read(char cbuf[], int off, int len) //以字節的方式把輸入的字符放入char數組中 off是起始位置,len是最大讀入的字節數。
//The character read, as an integer in the range 0 to 65535 (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has been reached
public int read()
BufferedReader的最大特點就是緩沖區的設置。通常Reader 所作的每個讀取請求都會導致對底層字符或字節流進行相應的讀取請求,如果沒有緩沖,則調用 read() 或 readLine() 都會導致從文件中讀取字節,並將其轉換為字符后返回,而這是極其低效的。使用BufferedReader可以指定緩沖區的大小,或者可使用默認的大小。大多數情況下,默認值就足夠大了。(如果我們是AC題的時候,在內存允許的情況下把緩存區設置為輸入的大小為最佳哈!!)
因此,建議用 BufferedReader 包裝所有其 read() 操作可能開銷很高的 Reader(如 FileReader 和InputStreamReader)
比如:
BufferedReader in= new BufferedReader(new FileReader("foo.in")) //將緩沖指定文件的輸入。
5.Scaner和BufferedReader區別
Scanner一個可以使用正則表達式來分析基本類型和字符串的簡單文本掃描器。
BufferedReader 從字符輸入流中讀取文本,緩沖各個字符,從而提供字符、數組和行的高效讀取。
- 1)Scanner 使用分隔符模式將其輸入分解為標記,默認情況下該分隔符模式與空白匹配。然后可以使用不同的 next 方法將得到的標記轉換為不同類型的值。
- 2)BufferedReader 可以指定緩沖區的大小,或者可使用默認的大小。大多數情況下,默認值就足夠大了,默認大小為8*1024 = 8192。
- 3)通常,Reader 所作的每個讀取請求都會導致對基礎字符或字節流進行相應的讀取請求。因此,建議用 BufferedReader 包裝所有其 read() 操作可能開銷很高的 Reader(如 FileReader 和 InputStreamReader)。
6.BufferedReader和InputStreamReader區別
BufferedReader的作用是針對帶有換行符的文本內容的按行讀取,同時要正確處理各種字符集的文本數據。BufferedReader一般創建時需要一個Reader的參數,由Reader去用流的方式讀取數據。而BufferedReader只是解析流數據並組成一行一行的String。"而InputStreamReader是Reader的一個子類 。
InputStreamReader中通過StreamDecoder這個輔助類來完成的。 "An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset"