【Java学习笔记】控制台读写

java

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

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/

以上是 【Java学习笔记】控制台读写 的全部内容, 来源链接: utcz.com/z/392351.html

回到顶部