java.io.Console的使用以及重定向…

在看console的时候  看到了个这个类只适用于标准的输入 输出流未被重定向的原始控制台中 比如 CMD中的那个对话框 在ECLIPSE中无法使用
  1. import java.io.Console;  
  2.   
  3.   
  4.   
  5. public class TestConsole  
  6.       
  7.     public static void main(String[] args)  
  8.           
  9.   
  10.         Console console=System.console();  
  11.           
  12.         if(console!=null 
  13.          
  14.             System.out.println("input data");  
  15.             String data=console.readLine();  
  16.             System.out.println("data="+data);  
  17.             char[] pwds=console.readPassword();  
  18.             System.out.println("pwds="+pwds);     
  19.             data=console.readLine("hello %s""test");  
  20.             System.out.println(data);  
  21.               
  22.             pwds=console.readPassword("hello password %s""test");  
  23.             System.out.println(pwds);  
  24.             //输出  
  25.             console.format("fuck %s\n""you");  
  26.               
  27.             console.writer().println("finish");  
  28.             console.flush();  
  29.               
  30.                       
  31.         }else 
  32.             System.out.println("console==null");  
  33.          
  34.      
  35.   
  36. }  

以上是这个类的具体命令 其实和System.out System.in 差不多
然后说这个重新定向 

Java的标准输入/输出分别通过System.in和System.out来代表,在默认的情况下分别代表键盘和显示器,当程序通过System.in来获得输入时,实际上是通过键盘获得输入。当程序通过System.out执行输出时,程序总是输出到屏幕。

在System类中提供了三个重定向标准输入/输出的方法

static void setErr(PrintStream err) 重定向“标准”错误输出流

static void setIn(InputStream in) 重定向“标准”输入流

static void setOut(PrintStream out)重定向“标准”输出流

下面程序通过重定向标准输出流,将System.out的输出重定向到文件输出,而不是在屏幕上输出。

意思就是把输出 输入的东西换了个方向

然后是代码

  1. import java.io.FileOutputStream;  
  2. import java.io.PrintStream;  
  3. public class Test  
  4.     public static void main(String[] args) throws Exception  
  5.      
  6.           
  7.         PrintStream ps=new PrintStream(new FileOutputStream("work"));  
  8.         System.setOut(ps);  
  9.         System.out.println("Hello World!");  
  10.   
  11.      
  12.       
  13.   
  14.   
  15.       
  16. }  
下面的代码将System.in重定向到文件输入,所以将不接受键盘输入
Java代码  收藏代码
  1. import java.io.FileInputStream;  
  2. import java.util.Scanner;  
  3.   
  4.   
  5. public class Test  
  6.     public static void main(String[] args) throws Exception  
  7.      
  8.         FileInputStream fis=new FileInputStream("work");  
  9.         System.setIn(fis);  
  10.           
  11.         Scanner sc=new Scanner(System.in);  
  12.         while(sc.hasNextLine())  
  13.          
  14.             System.out.println(sc.nextLine());  
  15.          
  16.           
  17.   
  18.      
  19.       
  20.   
  21.   
  22.       
  23. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值