import java.io.Console; public class ConsoleLogin { public static void main(String arg[]) { Console con; con = System.console(); if(con == null) { System.out.println("No console available."); return; } String name = con.readLine("name: "); char pw[] = con.readPassword("password: "); String password = new String(pw); System.out.println("The login name is " + name + " and the pass word is " + password); } } ====================== import java.io.Console; public class ConsoleOutput { public static void main(String arg[]) { Console con; con = System.console(); if(con == null) { System.out.println("No console available."); return; } int value = 31520; con.printf("The number %d as hexidecimal is %4X/n",value,value); } } 注意Console这个类的read和write方法是synchronized,而且console可能不存在(当然,你要是使用cmd或者shell则一定存在),最好使用flush刷新display
本文转自gnuhpc博客园博客,原文链接:http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822318.html,如需转载请自行联系原作者