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

static void setErr​(PrintStream err)
Reassigns the "standard" error output stream.(重定向“标准”错误输出流)
static void setIn​(InputStream in)
Reassigns the "standard" input stream.(重定向“标准”输入流)
static void setOut​(PrintStream out)
Reassigns the "standard" output stream.(重定向“标准”输出流)
static void setProperties​(Properties props)
Sets the system properties to the Properties argument.                                                                                                 
static String setProperty​(String key,String value)
Sets the system property indicated by the specified key.
static void setSecurityManager​(SecurityManager s)
Sets the System security.

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

  首先回顾PrintStream的构造器:

Constructor Description
PrintStream​(File file)
Creates a new print stream, without automatic line flushing, with the specified file.
PrintStream​(File file, String csn)
Creates a new print stream, without automatic line flushing, with the specified file and charset.
PrintStream​(OutputStream out)
Creates a new print stream.
PrintStream​(OutputStream out, boolean autoFlush)
Creates a new print stream.
PrintStream​(OutputStream out, boolean autoFlush,String encoding)
Creates a new print stream.
PrintStream​(String fileName)
Creates a new print stream, without automatic line flushing, with the specified file name.
PrintStream​(String fileName, String csn)
Creates a new print stream, without automatic line flushing, with the specified file name and charset.
 package com.zyjhandsome.io;

 import java.io.*;

 public class RedirectOut {

     public static void main(String[] args) {
// TODO Auto-generated method stub
try {
PrintStream ps = new PrintStream(new FileOutputStream("D:\\User_zhaoyingjun\\JavaSE\\Test\\RedirectOut.log"));
// 将标准的输出重定向到ps输出流
System.setOut(ps);
// 向标准输出输出一个字符串
System.out.println("普通字符串");
// 向标准输出输出一个对象
System.out.println(new RedirectOut());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  在“Redirect.log”文件中输出结果:

 普通字符串
com.zyjhandsome.io.RedirectOut@71be98f5

  下面程序重定向标准输入流,从而可以将System.in重定向到指定文件,而不是键盘输入。

 package com.zyjhandsome.io;

 import java.io.*;
import java.util.*; public class RedirectIn { public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileInputStream fis = new FileInputStream("D:\\User_zhaoyingjun\\JavaSE\\Java_Eclipse_Workspace\\HelloWorld2\\src\\com\\zyjhandsome\\io\\RedirectIn.java");
// 将标准输入流 重定向到fis输入流
System.setIn(fis);
// 使用System.in创建Scanner对象,用于获取标准输入
Scanner sc = new Scanner(System.in);
// 增加下面一行只把回车作为分隔符
sc.useDelimiter("\n");
// 判读是否还有下一个输入项
while (sc.hasNext())
{
// 输出输入项
System.out.println("键盘输入的内容是:" + sc.next());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

  输出结果:

 键盘输入的内容是:package com.zyjhandsome.io;

 键盘输入的内容是:

 键盘输入的内容是:import java.io.*;

 键盘输入的内容是:import java.util.*;

 键盘输入的内容是:

 键盘输入的内容是:public class RedirectIn {

 键盘输入的内容是:

 键盘输入的内容是:    public static void main(String[] args) {

 键盘输入的内容是:        // TODO Auto-generated method stub

 键盘输入的内容是:        try {

 键盘输入的内容是:            FileInputStream fis = new FileInputStream("D:\\User_zhaoyingjun\\JavaSE\\Java_Eclipse_Workspace\\HelloWorld2\\src\\com\\zyjhandsome\\io\\RedirectIn.java");

 键盘输入的内容是:            // 将标准输入流 重定向到fis输入流

 键盘输入的内容是:            System.setIn(fis);

 键盘输入的内容是:            // 使用System.in创建Scanner对象,用于获取标准输入

 键盘输入的内容是:            Scanner sc = new Scanner(System.in);

 键盘输入的内容是:            // 增加下面一行只把回车作为分隔符

 键盘输入的内容是:            sc.useDelimiter("\n");

 键盘输入的内容是:            // 判读是否还有下一个输入项

 键盘输入的内容是:            while (sc.hasNext())

 键盘输入的内容是:            {

 键盘输入的内容是:                // 输出输入项

 键盘输入的内容是:                System.out.println("键盘输入的内容是:" + sc.next());                

 键盘输入的内容是:            }            

 键盘输入的内容是:        } catch (FileNotFoundException e) {

 键盘输入的内容是:            // TODO Auto-generated catch block

 键盘输入的内容是:            e.printStackTrace();

 键盘输入的内容是:        }

 键盘输入的内容是:    }

 键盘输入的内容是:}

  上面程序创建了一个FileInputStream输入流,并使用System的setIn()方法将系统标准输入重定向到该文件输入流。运行上面程序,程序不会等待用户输入,而是直接输出了RedirectIn.java文件的内容,这表明程序不再使用键盘作为标准输入,而是使用RedirectIn.java文件作为标准输入源。

Java 输入/输出——重定向标准输入/输出的更多相关文章

  1. Java重定向标准输入/输出

    在System类中提供了三个重定向标准输入/输出的方法static void setErr(PrintStream err) 重定向“标准”错误输出流static void setIn(InputSt ...

  2. Linux学习笔记(十)shell基础:历史命令、命令补全、输出重定向、输出重定向

    一.历史命令 history [选项] [历史命令保存文件] -c 清空历史命令 -w 吧缓存中的历史命令写入历史命令保存文件~/.bash_history中 系统会默认将上次注销登录(正确退出)之前 ...

  3. [java]输入一个算术表达式输出结果

    动手有益. 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形.例如:  输入:3+8×2/9-2  输出:2 /** * input a calculate stri ...

  4. java输入一个字符串,输出该字符串的所有的排序

    public class Sort { public static void arrangeSequence(char[] strArr,int i){ char temp; ArrayList< ...

  5. java输出重定向

    Java的标准输入,输出分别是通过System.in和System.out来代表.默认情况下他们分别代表键盘和显示器. System类里提供了3个重定向标准输入,输出的方法. static void ...

  6. java SE :标准输入/输出

    一 标准设备输入/输出 A 标准输入/输出类 System B 控制台读写类 Console  标准输入/输出类  System 1 标准输入.标准输出.错误输出流 // 标准输入流 public f ...

  7. Linux Shell基础 Shell的输入重定向和输出重定向

    概述 在 Linux 中输入设备指的是键盘,输出设备指的是显示器.在 Linux 中,所有的内容都是文件,计算机硬件也是文件,标准输入设备(键盘)和标准输出设备(显示器)也是文件.这些设备的设备文件名 ...

  8. Shell(六):输入/输出重定向

    重定向的作用是将命令的执行结果输出到指定的文件中. 重定向命令列表如下: 文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR). 1.输出重 ...

  9. linux 输出重定向

    输出重定向 标准输入 文件描述符:0 设备:键盘 设备文件名:/dev/stdin 标准输出 文件描述符:1 设备:显示器 设备文件名:/dev/sdtout 标准输出重定向 命令 >> ...

随机推荐

  1. lua -- 物品的配置文件,表的形式保存

    do local goodsprop= { RECORDS={ []={ BuyGold= , RecoverPrice= , Value= , Param2= , UseTimesPerDay= , ...

  2. 开始逐步补充下相关Web知识,很多年没搞了....

    <script type="text/javascript"> $(function(){ ShowProduct(); $("#ShowUserInfo&q ...

  3. 解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

    从网上抓了一些字节流,想打印出来结果发生了一下错误: UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position ...

  4. “RESOURCE MONITOR“CPU占用特别高

    背景: SQL Server 2008 R2 10.50.1600 没有设置页面文件,内存为64G,数据库分配50G cpu使用占了50%以上,平时只有10-20%,某台服务器“RESOURCE MO ...

  5. --save与--save-dev的区别

    --save安装的包会在生产和开发环境中都使用: --save-dev的包只在开发环境中使用,在生产环境中就不需要这个包了,不再打包:

  6. Android开发(二十三)——Application

    参考: [1] Android中Application类用法.http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.htm ...

  7. mysql 5.7 学习

    MySQL5.7 添加用户.删除用户与授权   mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: ...

  8. Odoo 8 Graph 视图 之 雷达图 (Radar\Spider)

    据说7.0是有Radar图的,但是8以后被阉割掉了.自己动手 ,丰衣足食. 经过一天的努力,雷达图现已成功加入群共享套餐.

  9. Android——RecycleView

    RecycleView设置点击事件 http://blog.csdn.net/guxiao1201/article/details/40423361

  10. WPF 4.5 is here : check out the new features !

    So what's New in WPF Version 4.5 Developer Preview ? Here is a list of the new features and their re ...