在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文件作为标准输入源。