IO:重定向标准输出流

来源:互联网 发布:淘宝价格趋势 编辑:程序博客网 时间:2024/06/13 05:09

 

IO:重定向标准输出流

以下程序将System.out的输出重定向到文件输出,而不是屏幕上输出

package net.nyist.io;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;public class RedirectOut {public static void main(String[] args) {try (// 一次性创建PrintStream输出流PrintStream ps = new PrintStream(new FileOutputStream("out.txt"));) {//将标准输出重定向到ps输出流中System.setOut(ps);//向标准输出输出一个字符串System.out.println("普遍字符串");//向标准输出输出一个对象System.out.println(new RedirectOut());} catch (IOException ioe) {ioe.printStackTrace();}}}


 备注:

在System类中提供了3个重定标准输入、输出的方法

(1)static void setErr(PrintStream err);

(2)static void setIn(InputStream in);

(3)static void setOut(PrintStream out);

 

原创粉丝点击