console的使用及引申的输入输出重定向问题

为了解决Scanner类输入可见的问题,java SE 6 特别引入了Console类来实现这个目的,以下是Console的使用示例:

import java.io.Console;

import java.util.*;

public class  Welcome

{

public static void main(String[] args)

{

Console cons=System.console();

String username=cons.readLine("your name:");

char[] passwd=cons.readPassword("your password:");

System.out.println("hai! "+username+" this year,your password is "+passwd);

}

}

  但是采用console对象处理输入不如采用scanner方便,每次只能读取一行输入,而没有能够读取一个单词或一个数组的方法。而且,需要注意的一点是:Java.io.Console 只能用在标准输入、输出流未被重定向的原始控制台中使用,在 Eclipse 或者其他 IDE 的控制台是用不了的。

  对这句话的解释(Java.io.Console 只能用在标准输入、输出流未被重定向的原始控制台中使用,在 Eclipse 或者其他 IDE 的控制台是用不了的):一般情况下,System.in代表的是键盘、System.out是代表的控制台(显示器)。当程序通过System.in来获取输入的时候,默认情况下,是从键盘读取输入;当程序试图通过System.out执行输出时,程序总是输出到显示器。如果我们想对这样的情况做一个改变,例如获取输入时,不是来自键盘,而是来自文件或其他的位置;输出的时候,不是输出到显示器上显示,而是输出到文件或其他位置,这就是所谓的输入输出重定向。很明显,在 Eclipse 或者其他 IDE 的控制台已经进行了重定向。

  重定向的方法:

1.重定向的标准输出,Static void setOutPrintStream out

  /**

* 重定向标准输出流

* 1.初始化PrintStream对象

* 2.调用System.setOut()方法,将标准输出流重定向到PrintStraem对象

* 3.操作System.out

*/

public class ReOut {

public static void main(String[] args) throws FileNotFoundException {

//初始化一个PrintStream对象

PrintStream ps = new PrintStream(new FileOutputStream("c:/myDoc/hello.txt"));

//重定向标准输出流,重定向到上面指定的文件

System.setOut(ps);

//使用PrintStream对象向流中写信息

System.out.print("测试一下,看重定向是否成功");

//关闭流

ps.close();

}

}

2.重定向的标准输入

Static void setInInputStream in

/**

* 重定向标准输入流

* 1.有一个已经初始化的输入流InputStream

* 2.调用System.setIn()方法,将标准输入流重定向到目的输入流

* 3.System.in进行读取操作

* @author ghp

*

*/

 

public class ReIn {

public static void main(String[] args) throws IOException {

//实例化FileinputStream对象

FileInputStream fis = new FileInputStream("c:/myDoc/hello.txt");

//重定向标准输入流

System.setIn(fis);

//读取System.in标准输入流中的内容

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//输出System.in中的内容

String line = null;

System.out.println("==============开始读取标准输入流=============");

while((line = br.readLine()) !=null){

System.out.println(line);

}

//关闭流

br.close();

fis.close();

System.out.println("=============读取输入流完毕============");

}

 

}

相关链接:http://blog.csdn.net/yj_fq/article/details/41856073

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题,以下是关于 C# 输入输出重定向的方法一:同步 Process 的解答。 在 C# 中,可以使用 Process 类来执行外部程序,并且可以通过重定向输入和输出流来实现与外部程序的交互。具体步骤如下: 1. 创建 ProcessStartInfo 对象,设置 FileName 属性为要执行的外部程序的路径,设置 RedirectStandardInput 和 RedirectStandardOutput 属性为 true,表示重定向输入和输出流。 2. 创建 Process 对象,设置 StartInfo 属性为上述 ProcessStartInfo 对象,然后调用 Start 方法启动进程。 3. 使用 Process 对象的 StandardInput 属性获取重定向的输入流,使用 WriteLine 方法向外部程序写入数据。 4. 使用 Process 对象的 StandardOutput 属性获取重定向输出流,使用 ReadLine 方法从外部程序读取数据。 以下是一个简单的示例代码: ``` using System; using System.Diagnostics; class Program { static void Main() { var startInfo = new ProcessStartInfo { FileName = "外部程序路径", RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false }; var process = new Process { StartInfo = startInfo }; process.Start(); var standardInput = process.StandardInput; var standardOutput = process.StandardOutput; standardInput.WriteLine("要写入的数据"); var output = standardOutput.ReadLine(); Console.WriteLine(output); process.WaitForExit(); } } ``` 以上就是 C# 输入输出重定向方法一:同步 Process 的解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值