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); } }
时间: 2024-10-10 04:47:04