勿以惡小而爲之,勿以善小而不爲--------------------------劉備
勸諸君,多行善事積福報,莫作惡
上一章簡單介紹了 打印輸出流PrintStream和PrintWriter(五),如果沒有看過,請觀看上一章
一. Scanner 類
java.util.Scanner 類是 jdk1.5 之後出現的, 主要是方便讀取 控制檯輸入的數據。當然,也可以讀取文件的內容,但常用於接收控制檯輸入。
以前,我們要想接收控制檯的輸入信息,需要用到 System.in 的純 InputStream情況。
如接收用戶的年齡:
//注意,是字節讀取,會存在亂碼情況
public static void inTest() throws Exception{
System.out.println("請輸入你的年齡:");
InputStream inputStream= System.in;
byte[] bytes=new byte[6];
//將輸入的內容放置到 bytes 字節數組裏
int len=inputStream.read(bytes);
String ageString=new String(bytes,0,len);
int age=Integer.parseInt(ageString);
System.out.println("用戶的年齡是:"+age);
}
單單的接收一個年齡,就需要很多步驟,如果接收多個信息,則需要循環實例化讀取,當接收中文字符串的時候,還需要處理中文亂碼的問題。
很麻煩, 但是使用 新的 Scanner 類時,就非常方便了。
一.一 構造方法
一.一.一 方法
方法 | 作用 |
---|---|
Scanner(File source, String charsetName) | 傳入文件和 字符編碼 |
Scanner(InputStream source, String charsetName) | 傳入InputStream 和 字符編碼 |
Scanner(Path source, String charsetName) | 傳入Path 對象和字符編碼 |
一般用的是 InputStream 參數。
一.一.二 演示
@Test
public void conTest() throws Exception{
//第一種方式,傳入文件 File
File file=new File("E:"+ File.separator+"ideaWork"+File.separator+"Java2"+File.separator+"fileSrc"
+File.separator+"out.txt");
Scanner scanner=new Scanner(file);
//2. 第二種,傳入 InputStream
InputStream inputStream=new FileInputStream(file);
Scanner scanner1=new Scanner(inputStream);
//一般使用的是 System.in
Scanner scanner2=new Scanner(System.in);
//第三種, 還可以傳入 Path
Path path= Paths.get("E:","ideaWork"+ File.separator+"Java2"+File.separator+"fileSrc"
+File.separator+"Hello.txt");
Scanner scanner3=new Scanner(path);
}
一.二 普通方法
Scanner 的方法有很多,只列舉幾個常用的。
方法 | 作用 |
---|---|
void close() | 關閉此掃描儀。 |
接收數據: | |
nextInt() | 接收數據,並轉換成int 類型 |
nextFloat() | 接收數據,並轉換成 Float類型 |
String next() | 接收數據,並轉換成字符串,接收數據時,空格結束 |
String nextLine() | 接收數據,並轉換成字符串, 接收數據時,回車鍵結束 |
–判斷數據 | |
boolean hasNextInt() | 判斷傳入的數據,是否能轉換成int類型 |
boolean hasNextFloat() | 判斷傳入的數據,是否能轉換成float 類型 |
boolean hasNext() | 判斷是否有下一個數據 |
boolean hasNextLine() | 判斷是否有下一行數據 |
boolean hasNext(String pattern) | 判斷是否符合 正則表達式 |
Scanner useDelimiter(String pattern) | 設置結束讀取的標識符 |
二. 演示Scanner
二.一 讀取單個數據 如 年齡
讀取年齡時,用單純的InputStream 時,會發現很難處理。 但是用 Scanner 時:
//接收前端傳入的數據
public static void main(String[] args) {
try {
inputTest();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void inputTest() throws Exception{
Scanner scanner=new Scanner(System.in);
//1. 輸入name
System.out.println("請輸入年齡:");
//會自動轉換
int age=scanner.nextInt();
System.out.println("您輸入的年齡是:"+age);
scanner.close();
}
運行程序,控制檯輸入年齡 24
會自動轉換成相應的int 類型。
如果你輸入的不是 數字型字符串,那麼將會轉換失敗, 拋出 java.util.InputMismatchException 類型。
所以,通常在接收之前,會驗證傳入數據的合法性。
二.二 接收前驗證數據
用 hasNextXxx() 進行驗證。 當然,也可以自己寫正則表達式進行驗證
//main 方法中 調用 這個方法
public static void input1Test() throws Exception{
Scanner scanner=new Scanner(System.in);
//1. 輸入name
System.out.println("請輸入年齡:");
if(scanner.hasNextInt()){
int age=scanner.nextInt();
System.out.println("您輸入的年齡是:"+age);
}else{
System.out.println("請輸入正確的年齡數字");
}
scanner.close();
}
當輸入非數字時,
二.三 重複接收不同類型的數據
public static void input2Test() throws Exception{
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入年齡:");
//1. 接收 int
int age=scanner.nextInt();
System.out.println("請輸入成績:");
//接收 float
float score=scanner.nextFloat();
System.out.println("請輸入姓名:");
//接收 字符串
String name=scanner.next();
System.out.println("姓名是:"+name+",年齡是:"+age+",成績是:"+score);
scanner.close();
}
可以用 next() 也可以用 nextLine() 進行接收字符串,不會造成中文亂碼問題。 但是,這兩者有什麼區別呢?
next() 接收數據,是按照空格進行拆分的,每次讀取時,讀取到空格時會結束本次讀取。
而 nextLine() 接收數據時,是按照回車鍵進行拆分的,每次讀取時,讀取到回車鍵時會結束本次讀取。
二.四 next() 與 nextLine() 的區別
public static void input3Test() throws Exception{
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入內容:");
String n1=scanner.next();
System.out.println("輸出內容next接收:"+n1);
/*String n2=scanner.nextLine();
System.out.println("輸出內容 nextLine接收:"+n2);*/
scanner.close();
}
當用 next() 進行接收時,
發現,只會讀取空格前面的 我是。
當用 nextLine() 進行接收時,
兩者的結束標識是不一樣的, 這個標識是可以自動設置的。 useDelimiter()
public static void input3Test() throws Exception{
Scanner scanner=new Scanner(System.in);
System.out.println("請輸入內容:");
scanner.useDelimiter("\n");
String n1=scanner.next();
System.out.println("輸出內容next接收:"+n1);
/* String n2=scanner.nextLine();
System.out.println("輸出內容 nextLine接收:"+n2);*/
scanner.close();
}
這個時候,再輸入空格時, 就不會結束了。
二.五 讀取文件的內容
讀取時,按行讀取,需要判斷是否還有下一行, 如果有,就讀取。
傳入參數,最好傳入輸入流, 統一。
public static void input4Test() throws Exception{
File file=new File("E:"+ File.separator+"ideaWork"+File.separator+"Java2"+File.separator+"fileSrc"
+File.separator+"out.txt");
InputStream inputStream=new FileInputStream(file);
Scanner scanner=new Scanner(inputStream);
int i=0;
//當還有數據時
while(scanner.hasNextLine()){
String line=scanner.nextLine();
System.out.println((++i)+":"+line);
}
scanner.close();
}
運行程序:
謝謝您的觀看,如果喜歡,請關注我,再次感謝 !!!