Java 输入/输出——重定向标准输入/输出
在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 输入/输出——重定向标准输入/输出的更多相关文章
- Java重定向标准输入/输出
在System类中提供了三个重定向标准输入/输出的方法static void setErr(PrintStream err) 重定向“标准”错误输出流static void setIn(InputSt ...
- Linux学习笔记(十)shell基础:历史命令、命令补全、输出重定向、输出重定向
一.历史命令 history [选项] [历史命令保存文件] -c 清空历史命令 -w 吧缓存中的历史命令写入历史命令保存文件~/.bash_history中 系统会默认将上次注销登录(正确退出)之前 ...
- [java]输入一个算术表达式输出结果
动手有益. 输入一个表达式,没有括号,数字小于0-9之间,输出计算结果,所有的中间结果化为整形.例如: 输入:3+8×2/9-2 输出:2 /** * input a calculate stri ...
- java输入一个字符串,输出该字符串的所有的排序
public class Sort { public static void arrangeSequence(char[] strArr,int i){ char temp; ArrayList< ...
- java输出重定向
Java的标准输入,输出分别是通过System.in和System.out来代表.默认情况下他们分别代表键盘和显示器. System类里提供了3个重定向标准输入,输出的方法. static void ...
- java SE :标准输入/输出
一 标准设备输入/输出 A 标准输入/输出类 System B 控制台读写类 Console 标准输入/输出类 System 1 标准输入.标准输出.错误输出流 // 标准输入流 public f ...
- Linux Shell基础 Shell的输入重定向和输出重定向
概述 在 Linux 中输入设备指的是键盘,输出设备指的是显示器.在 Linux 中,所有的内容都是文件,计算机硬件也是文件,标准输入设备(键盘)和标准输出设备(显示器)也是文件.这些设备的设备文件名 ...
- Shell(六):输入/输出重定向
重定向的作用是将命令的执行结果输出到指定的文件中. 重定向命令列表如下: 文件描述符 0 通常是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR). 1.输出重 ...
- linux 输出重定向
输出重定向 标准输入 文件描述符:0 设备:键盘 设备文件名:/dev/stdin 标准输出 文件描述符:1 设备:显示器 设备文件名:/dev/sdtout 标准输出重定向 命令 >> ...
随机推荐
- supervisor详解
1.什么是supervisor supervisor是用python写的一个进程管理工具,用来启动,重启,关闭进程. 2.supervisor的安装 pip install supervisor 3. ...
- Selenium和firefox兼容性问题
Selenium和firefox兼容性问题 2016-07-10 若出现兼容性问题,会报如下错误: org.openqa.selenium.firefox.NotConnectedException: ...
- 基音检测算法的性能:Performance Evaluation of Pitch Detection Algorithms
http://access.feld.cvut.cz/view.php?cisloclanku=2009060001 Vydáno dne 02. 06. 2009 (15123 přečtení) ...
- 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-13 emWin底层驱动接口介绍
视频简介:该视频介绍emWin底层驱动接口. 源视频包下载地址:链接:http://pan.baidu.com/s/1nvPpC2d 密码:cbb7 银杏科技优酷视频发布区:http://i.youk ...
- github新建repositories后import已有code 随后同步更新
github上,可以forks别人已有的项目,而且同步更新合并也很方便,但如果是自己新建的项目,而导入的是别人的代码修改后,别人的又更新了,自己想获取他的更新,怎么办呢?其实很简单. # git cl ...
- PwniumCTF2014 - JJSN总结
Write-ups 本文最早发布在TSRC,详细地址:http://security.tencent.com/index.php/blog/msg/55 Forensics USB is FUN 这道 ...
- 【css】css 中文字体 unicode 对照表
css 中文字体可以用 unicode 格式来表示,比如“宋体”可以用 \5B8B\4F53 来表示.具体参考下表: 中文名 英文名 unicode 宋体 SimSun \5B8B\4F53 黑体 S ...
- Oracle HAVING子句 - 转
使用 HAVING 子句选择行 HAVING 子句对 GROUP BY 子句设置条件的方式与 WHERE 子句和 SELECT 语句交互的方式类似.WHERE 子句搜索条件在进行分组操作之前应用:而 ...
- AES和RSA算法的demo代码
aes代码示例: package com.autoyol.util.security.test; import java.security.Key; import java.security.NoSu ...
- 根据key删除Map集合中的key-value映射
一:在遍历Map时是不可以删除key-value映射的,如果根据key删除,如下: public static void main(String[] args) { Map<String,Obj ...