在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. .NET开发微信公众号之创建自定义菜单

    一.简介 微信公众平台服务号以及之前成功申请内测资格的订阅号都具有自定义菜单的功能.开发者可利用该功能为公众账号的会话界面底部增加自定义菜单,用户点击菜单中的选项,可以调出相应的回复信息或网页链接.自 ...

  2. 常用七种排序的python实现

    1 算法复杂度 算法复杂度分为时间复杂度和空间复杂度.其中, 时间复杂度是指执行算法所需要的计算工作量:而空间复杂度是指执行这个算法所需要的内存空间. 算法的复杂性体现在运行该算法时的计算机所需资源的 ...

  3. MySql-Binlog协议详解

    Reference: https://blog.csdn.net/hj7jay/article/details/56665057?utm_source=blogxgwz7 MySql-Binlog协议 ...

  4. html网页采集

    UI_Less.pas: unit UI_Less; interface uses Windows, Classes, Messages, Forms, MsHtml, Urlmon, ActiveX ...

  5. 【iCore1S 双核心板_FPGA】例程十五:基于I2C的ARM与FPGA通信实验

    实验现象: 核心代码: int main(void) { int i,n; ]; ]; HAL_Init(); system_clock.initialize(); led.initialize(); ...

  6. CSS初始化示例代码

    CSS初始化示例代码 /* css reset www.admin10000.com */ body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code, ...

  7. error C3861: “xxxx”: 找不到标识符

    问题出现背景:c++静态类库中主函数里包含子函数 解决办法:先声明子函数再写主函数.

  8. 字符串时间与Unix时间戳相互转换

    字符串时间与Unix时间戳相互转换 /** * @Author: wangkun * @Date : 2016/1/21 13:43 * @Description : 字符串时间转换为Unix时间戳 ...

  9. java获取视频缩略图

    近期由于在做一个关于视频播放的项目,需要使用程序自动获取视频文件的缩略图,特写此文供其他人参考,有不清楚之楚可以给我留言. 1.使用工具:ffmpeg, 官网下载地址:http://ffmpeg.or ...

  10. eclipse安装emmet插件

    http://www.cnblogs.com/matchless/archive/2013/04/10/3011973.html