一,利用 Scanner 實現從鍵盤讀入integer或float 型數據
1 import java.util.*; 2 public class test { 3 public static void main(String args[]) 4 { 5 Scanner in=new Scanner(System.in); //使用Scanner類定義對象 6 System.out.println("please input a float number"); 7 float a=in.nextFloat(); //接收float型數據 8 System.out.println(a); 9 System.out.println("please input a integer number"); 10 int b=in.nextInt(); //接收整形數據 11 System.out.println(b); 12 } 13 }
二,利用 BufferedReader實現從鍵盤讀入字符串並寫進文件abc.txt中
1 import java.io.*; 2 public class Test 3 { 4 public static void main(String[] args) throws IOException 5 { 6 BufferedReader buf = new BufferedReader (new InputStreamReader(System.in)); 7 BufferedWriter buff = new BufferedWriter(new FileWriter("abc.txt")); 8 String str = buf.readLine(); 9 while(!str.equals("exit")) 10 { 11 buff.write(str); 12 buff.newLine(); 13 str = buf.readLine(); 14 } 15 buf.close(); 16 buff.close(); 17 } 18 }
關於JDK1.5 Scanner類的說明
Scanner是SDK1.5新增的一個類,可是使用該類創建一個對象.
Scanner reader=new Scanner(System.in);
然后reader對象調用下列方法(函數),讀取用戶在命令行輸入的各種數據類型:
next.Byte(),nextDouble(),nextFloat,nextInt(),nextLine(),nextLong(),nextShot()
使用nextLine()方法輸入行中可能包含空格.如果讀取的是一個單詞,則可調用
.next()方法
三、Scanner和BufferedReader的區別
在命令行模式下要輸入數據至程序中時,我們可以使用標准輸入串對象System.in.但是,我們並不經常直接使用它,因為System.in提供的 read方法每次只能讀取一個字節的數據,而我們平時所應用的通常是讀取一個字符串或者是一個數字,所以read方法所以提供的功能,對我們來說並沒有太大的用處.
在Java SE 6中,可以使用Scanner類取得用戶的輸入,Scanner類位於java.util包中,如果你要使用Scanner取得用戶輸入的話,要加上 import java.util.Scanner;這條語句.import的功能是告訴編譯器,你將使用java.util包中的Scanner類.
1 import java.util.Scanner; 2 public class Test 3 { 4 public static void main(String[] args) 5 { 6 Scanner scan = new Scanner(System.in); 7 System.out.println("請輸入一個字符串:"); 8 System.out.println("您輸入的字符串是:" + scan.next()); 9 } 10 }
運行上面的程序,你將會看到你輸入的字符串將在下面原樣顯示出來.
我們來看看這個程序中每條語句的意思:
new是創建一個對象,程序中new的意思是創建了一個Scanner類的對象scan.但是在創建Scanner類的對象時,需要用System.in 作為它的參數,也可以將Scanner看作是System.in對象的支持者,System.in取得用戶輸入的內容后,交給Scanner來作一些處理.
Scanner類中提供了多個方法:
next():取得一個字符串;
nextInt():將取得的字符串轉換成int類型的整數;
nextFloat():將取得的字符串轉換成float型;
nextBoolean():將取得的字符串轉換成boolean型;
用Scanner獲得用戶的輸入非常的方便,但是Scanner取得輸入的依據是空格符,包括空格鍵,Tab鍵和Enter鍵.當按下這其中的任一鍵時,Scanner就會返回下一個輸入. 當你輸入的內容中間包括空格時,顯然,使用Scanner就不能完整的獲得你輸入的字符串.這時候我們可以考慮使用BufferedReader類取得輸入.其實在Java SE 1.4及以前的版本中,尚沒有提供Scanner方法,我們獲得輸入時也是使用BufferReader的.
BufferedReader類位於java.io包中,所以要使用這個類,就要引入java.io這個包:import java.io.BufferedReader.
使用BufferedReader對象的readLine()方法必須處理java.io.IOException異常(Exception).
使用BufferedReader來取得輸入,理解起來要復雜得多.但是使用這個方法是固定的,每次使用前先如法炮制就可以了.
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String text = buffer.readLine();
readLine()方法會返回用戶在按下Enter鍵之前的所有字符輸入,不包括最后按下的Enter返回字符.
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 public class TestBufferedReader 5 { 6 public static void main(String[] args) throws IOException 7 { 8 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); 9 System.out.println("請輸入一串字符串"); 10 String text = buffer.readLine(); 11 System.out.println("您輸入的字符串是:" + text); 12 } 13 }
四,利用 System.in.read 實現從鍵盤讀入char 型數據
1 System.out.println("please input a char"); 2 char c=(char)System.in.read(); 3 System.out.println(c);