欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java的Console类的使用方法及实例

程序员文章站 2023-11-12 19:25:22
java的console类的使用方法及实例 jdk 6中提供了java.io.console类专用来访问基于字符的控制台设备。如果你的java程序要与windows下的c...

java的console类的使用方法及实例

jdk 6中提供了java.io.console类专用来访问基于字符的控制台设备。如果你的java程序要与windows下的cmd或者linux下的terminal交互,就可以用这个java console类代劳。

import java.io.console; 
import java.io.printwriter; 
 
public class testconsole { 
 
  public static void main(string[] args) { 
    console cons = system.console(); 
    if (cons != null) { 
      // ------------------------- 
      printwriter printwriter = cons.writer(); 
      printwriter.write("input:"); 
      cons.flush(); 
      // ------------------------- 
      string str1 = cons.readline(); 
      // ------------------------- 
      cons.format("%s", str1); 
    } 
  } 
} 

java.io.console 只能用在标准输入、输出流未被重定向的原始控制台中使用,在 eclipse 或者其他 ide 的控制台是用不了的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!