在Java中,向控制檯輸出內容很容易,只要通過如下語句就可以在控制檯輸出內容
System.out.println("This is test text!");
如果要想在控制檯獲取輸入,在Java中,必須先創建一個Scanner(JDK >= 1.5)對象,並將之與標準輸入流相關聯
Scanner in = new Scanner(System.in);
String name = in.nextLine();
注意:Scanner不止有nextLine方法,還有next、nextInt、nextDouble等方法。
下面的程序爲使用Scanner在控制檯讀取輸入的例子
import java.util.*; / ** * This program is console input * @author keyman * @version 1.0.0 2015-8-8 * */ public class InputTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String name = ""; int age = 0; System.out.print("what's your name? "); Scanner in = new Scanner(System.in); name = in.nextLine(); System.out.print("how old are you? "); age = in.nextInt(); System.out.println("hello, "+name+". next year, you'll be "+(age + 1)); } } ************************此處爲程序運行的情況************************************* what's your name? keyman how old are you? 20 hello, keyman. next year, you'll be 21 ********************************************************************************* 說明:Scanner有時候還是很方便的,例如如下例子就是使用Scanner從文件讀取輸入 import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; /** * This program is console input * @author keyman * @version 1.0.0 2015-8-8 * */ public class InputTest { /** * @param args */ public static void main(String[] args) { //此段代碼爲向c:\\console.txt中寫入內容 try{ FileWriter filewriter = new FileWriter("c:\\console.txt"); filewriter.write("1 2 3 4 5 6 7 8 9 last, 這是我的測試文本。"); filewriter.close(); }catch(IOException e) { System.out.println("file console.txt write is exception..."); } //此段代碼爲從c:\\console.txt讀取內容並輸出 FileReader filereader = null; try { filereader = new FileReader("c:\\console.txt"); Scanner scanner = new Scanner(filereader); while (scanner.hasNext()) { System.out.print(scanner.next()); } } catch (FileNotFoundException e) { throw new RuntimeException("File " + "c:\\console.txt" + " not found!"); } finally { if (filereader != null) try { filereader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
不難看出,以上從控制檯讀取輸入可以實現,但是全是明文,不適合輸入密碼等信息。在JDK1.6引入Console類來實現在控制檯輸入密碼的需求
注意:不總是能得到可用的Console, 一個JVM是否有可用的Console依賴於底層平臺和JVM如何被調用.
如果JVM是在交互式命令行(比如Windows的cmd)中啓動的,並且輸入輸出沒有重定向到另外的地
方,那麼就可以得到一個可用的Console實例。
使用Console類實現讀取密碼的例子如下
import java.io.Console; import java.util.*; /** * This program is console input * @author keyman * @version 1.0.0 2015-8-8 * */ public class InputTest { /** * @param args */ public static void main(String[] args) { try{ Console co = System.console(); String name = co.readLine("what's your name? "); char [] password = co.readPassword("Your password is? "); System.out.print("hello, "+name+"."+"Your input password is "); for (int i=0;i<password.length;i++) { System.out.print(password[i]); } }catch(NullPointerException e){ System.out.println("Console is not available"); //e.printStackTrace(); } } } ************************此處爲程序運行的情況************************************* what's your name? keyman Your password is? hello, keyman.Your input password is keyman *********************************************************************************
接下來再來講解另一個從控制檯獲取輸入的方法,此方法在各個版本均被支持。即直接使用System.in
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * This program is console input * @author keyman * @version 1.0.0 2015-8-8 * */ public class InputTest { /** * @param args */ public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; try { System.out.print("Please input some string: "); str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } System.out.println("The input information from console ==> " + str); } }
總結:在Java中、從控制檯獲取輸入的方式有三種,具體如下
Java.lang.System.in (目前JDK版本均支持)
Java.util.Scanner (JDK版本>=1.5)
Java.io.Console(JDK版本>=1.6) ==> 通常用於讀入密碼,因爲不回顯字符
注:本文僅爲本人膚淺的學習,如有紕漏,望請指正!同時、本文僅供參考!