锘?!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
銆銆灞變笢杞歡寮鍙?/strong>鍦↗ava 瀛楃緇堢涓婅幏鍙栬緭鍏ユ湁涓夌鏂瑰紡錛?/span>
銆銆1銆乯ava.lang.System.in (鐩墠JDK鐗堟湰鍧囨敮鎸?
銆銆2銆乯ava.util.Scanner (JDK鐗堟湰>=1.5)
銆銆3銆乯ava.io.Console(JDK鐗堟湰>=1.6)錛岀壒鑹詫細鑳戒笉鍥炴樉瀵嗙爜瀛楃
銆銆鍙傝冿細
銆銆榪欓噷璁板綍Java涓粠鎺у埗鍙拌鍏ヤ俊鎭殑鍑犵鏂瑰紡
銆銆(1)JDK 1.4(JDK 1.5鍜孞DK 1.6涔熼兘鍏煎榪欑鏂規硶)
銆銆public class TestConsole1 {
銆銆public static void main(String[] args) {
銆銆String str = readDataFromConsole("Please input string錛?;
銆銆System.out.println("The information from console錛?+ str);
銆銆}
銆銆/**
銆銆* Use InputStreamReader and System.in to read data from console
銆銆*
銆銆* @param prompt
銆銆*
銆銆* @return input string
銆銆*/
銆銆private static String readDataFromConsole(String prompt) {
銆銆BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
銆銆String str = null;
銆銆try {
銆銆System.out.print(prompt);
銆銆str = br.readLine();
銆銆} catch (IOException e) {
銆銆e.printStackTrace();
銆銆}
銆銆return str;
銆銆}
銆銆}
銆銆(2)JDK 1.5錛堝埄鐢⊿canner榪涜璇誨彇錛?/span>
銆銆public class TestConsole2 {
銆銆public static void main(String[] args) {
銆銆String str = readDataFromConsole("Please input string:");
銆銆System.out.println("The information from console:" + str);
銆銆}
銆銆/**
銆銆* Use java.util.Scanner to read data from console
銆銆*
銆銆* @param prompt
銆銆*
銆銆* @return input string
銆銆*/
銆銆private static String readDataFromConsole(String prompt) {
銆銆Scanner scanner = new Scanner(System.in);
銆銆System.out.print(prompt);
銆銆return scanner.nextLine();
銆銆}
銆銆}
銆銆Scanner榪樺彲浠ュ緢鏂逛究鐨勬壂鎻忔枃浠訛紝璇誨彇閲岄潰鐨勪俊鎭茍杞崲鎴愪綘瑕佺殑綾誨瀷錛屾瘮濡傚鈥? 2.2 3.3 3.33 4.5 done鈥濊繖鏍風殑鏁版嵁姹傚拰,瑙佸涓嬩唬鐮?
銆銆public class TestConsole4 {
銆銆public static void main(String[] args) throws IOException {
銆銆FileWriter fw = new FileWriter("num.txt");
銆銆fw.write("2 2.2 3.3 3.33 4.5 done");
銆銆fw.close();
銆銆System.out.println("Sum is "+scanFileForSum("num.txt"));
銆銆}
銆銆public static double scanFileForSum(String fileName) throws IOException {
銆銆double sum = 0.0;
銆銆FileReader fr = null;
銆銆try {
銆銆fr = new FileReader(fileName);
銆銆Scanner scanner = new Scanner(fr);
銆銆while (scanner.hasNext()) {
銆銆if (scanner.hasNextDouble()) {
銆銆sum = sum + scanner.nextDouble();
銆銆} else {
銆銆String str = scanner.next();
銆銆if (str.equals("done")) {
銆銆break;
銆銆} else {
銆銆throw new RuntimeException("File Format is wrong!");
銆銆}
銆銆}
銆銆}
銆銆} catch (FileNotFoundException e) {
銆銆throw new RuntimeException("File " + fileName + " not found!");
銆銆} finally {
銆銆if (fr != null)
銆銆fr.close();
銆銆}
銆銆return sum;
銆銆}
銆銆}
銆銆(3)JDK 1.6錛堝埄鐢╦ava.io.Console榪涜璇誨彇錛?/span>
銆銆JDK6涓彁渚涗簡java.io.Console綾諱笓鐢ㄦ潵璁塊棶鍩轟簬瀛楃鐨勬帶鍒跺彴璁懼.
銆銆浣犵殑紼嬪簭濡傛灉瑕佷笌Windows涓嬬殑cmd鎴栬匧inux涓嬬殑Terminal浜や簰,灝卞彲浠ョ敤Console綾諱唬鍔?錛堢被浼糞ystem.in鍜孲ystem.out錛?/span>
銆銆浣嗘垜浠笉鎬繪槸鑳藉緱鍒板彲鐢ㄧ殑Console, 涓涓狫VM鏄惁鏈夊彲鐢ㄧ殑Console渚濊禆浜庡簳灞傚鉤鍙板拰JVM濡備綍琚皟鐢?
銆銆濡傛灉JVM鏄湪浜や簰寮忓懡浠よ(姣斿Windows鐨刢md)涓惎鍔ㄧ殑,騫朵笖杈撳叆杈撳嚭娌℃湁閲嶅畾鍚戝埌鍙﹀鐨勫湴鏂?閭d箞灝卞彲浠ュ緱鍒頒竴涓彲鐢ㄧ殑Console瀹炰緥銆?/span>
銆銆鍦ㄤ嬌鐢?IDE 鐨勬儏鍐典笅錛屾槸鏃犳硶鑾峰彇鍒癈onsole瀹炰緥鐨勶紝鍘熷洜鍦ㄤ簬鍦?IDE 鐨勭幆澧冧笅錛岄噸鏂板畾鍚戜簡鏍囧噯杈撳叆鍜岃緭鍑烘祦錛屼篃鏄氨鏄皢緋葷粺鎺у埗鍙頒笂鐨勮緭鍏ヨ緭鍑洪噸瀹氬悜鍒頒簡 IDE 鐨勬帶鍒跺彴涓?/span>
銆銆public class TestConsole3 {
銆銆public static void main(String[] args) {
銆銆String str = readDataFromConsole("Please input string:");
銆銆System.out.println("The information from console:" + str);
銆銆}
銆銆/**
銆銆* Use java.io.console to read data from console
銆銆*
銆銆* @param prompt
銆銆*
銆銆* @return input string
銆銆*/
銆銆private static String readDataFromConsole(String prompt) {
銆銆Console console = System.console();
銆銆if (console == null) {
銆銆throw new IllegalStateException("Console is not available!");
銆銆}
銆銆return console.readLine(prompt);
銆銆}
銆銆}
銆銆Console綾昏繕鏈変釜鐗硅壊灝辨槸錛屼笓闂ㄥ瀵嗙爜(杈撳叆鏃犲洖鏄?絳夊畨鍏ㄥ瓧絎︼紝榪涜浜嗗鐞嗐備笓闂ㄦ彁渚?readPassword()鏂規硶,鍏蜂綋搴旂敤瑙佸涓嬩唬鐮侊細
銆銆public class TestConsole5 {
銆銆public static void main(String[] args) {
銆銆Console console = System.console();
銆銆if (console == null) {
銆銆throw new IllegalStateException("Console is not available!");
銆銆}
銆銆while(true){
銆銆String username = console.readLine("Username: ");
銆銆char[] password = console.readPassword("Password: ");
銆銆if (username.equals("Chris") && String.valueOf(password).equals("GoHead")) {
銆銆console.printf("Welcome to Java Application %1$s.\n", username);
銆銆// 浣跨敤鍚庡簲绔嬪嵆灝嗘暟緇勬竻絀猴紝浠ュ噺灝戝叾鍦ㄥ唴瀛樹腑鍗犵敤鐨勬椂闂達紝澧炲己瀹夊叏鎬?/span>
銆銆password = null;
銆銆System.exit(-1);
銆銆}
銆銆else {
銆銆console.printf("Invalid username or password.\n");
銆銆}
銆銆}
銆銆}
銆銆}