標籤:
http://blog.csdn.net/hongweigg/article/details/14448731
在Java 字元終端上擷取輸入有三種方式:
1、java.lang.System.in (目前JDK版本均支援)
2、java.util.Scanner (JDK版本>=1.5)
3、java.io.Console(JDK版本>=1.6),特色:能不回顯密碼字元
package com.srie.chapter01;import java.io.BufferedReader;import java.io.Console;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;public class TestCalc { public static void main(String[] args) throws Exception { readBySystem(); readByScanner(); readByConsole(); } private static void readByConsole() { System.out.println("read by console"); Console console = System.console(); if(console == null){ System.out.println("console is null!"); }else{ String readLine = console.readLine(); System.out.println(readLine); } } private static void readBySystem() throws IOException { System.out.println("read by system.in"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); String readLine = bufferedReader.readLine(); System.out.println(readLine); } private static void readByScanner() { System.out.println("read by scanner:"); Scanner scanner = new Scanner(System.in); String nextLine = scanner.nextLine(); System.out.println(nextLine); }}
Java 字元終端上擷取輸入三種方式