讀取一個鍵盤錄入的數據,並打印在控制台上。 鍵盤本身就是一個標準的輸入設備, 對於 Java 而言,對於這種輸入設備都有對應的對象。
import java.io.IOException;
import java.io.InputStream;
public class ReadKeyDemo {
public static void main(String[] args) throws IOException {
// readKey();
readKey_2();
}
/*
* 獲取用戶鍵盤錄入的功能,
* 並將數據變成大寫顯示在控制台上,
* 如果用戶輸入的是 over, 結束鍵盤錄入。
*
* 思路:
* 1. 因為鍵盤錄入指讀取一個字節,要判斷是否是 over,需要將讀取到的字節拼成字符串。
* 2. 那就需要一個容器,StringBuilder
* 3. 在用戶回車之前將錄入的數據變成字符串即可。
*/
public static void readKey_2() throws IOException {
//1. 創建容器
StringBuilder sb = new StringBuilder();
//2. 獲取鍵盤讀取流
InputStream in = System.in;
//定義變量紀錄讀取到的字節,並循環獲取
int ch = 0;
while((ch = in.read()) != -1) {
//在存儲之前需要判斷是否是換行標記,因為換行標記不存儲
if(ch == '\r')
continue;
if(ch == '\n') {
String temp = sb.toString();
if("over".equals(temp)) {
break;
} else {
System.out.println(temp.toUpperCase());
sb.delete(0, sb.length());
}
} else {
//將讀取到的字節存儲到 StringBuilder 中
sb.append((char)ch);
// System.out.println(ch);
}
}
}
public static void readKey() throws IOException {
InputStream in = System.in;
int ch = in.read();//阻塞式方法
System.out.println(ch);
// in.close();//默認的輸入設備不需要關流
}
}