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.
 1 package com.zyjhandsome.io;
 2 
 3 import java.io.*;
 4 
 5 public class RedirectOut {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         try {
10             PrintStream ps = new PrintStream(new FileOutputStream("D:\\User_zhaoyingjun\\JavaSE\\Test\\RedirectOut.log"));
11             // 将标准的输出重定向到ps输出流
12             System.setOut(ps);
13             // 向标准输出输出一个字符串
14             System.out.println("普通字符串");
15             // 向标准输出输出一个对象
16             System.out.println(new RedirectOut());
17         } catch (FileNotFoundException e) {
18             // TODO Auto-generated catch block
19             e.printStackTrace();
20         } 
21     }
22 }

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

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

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

 1 package com.zyjhandsome.io;
 2 
 3 import java.io.*;
 4 import java.util.*;
 5 
 6 public class RedirectIn {
 7 
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         try {
11             FileInputStream fis = new FileInputStream("D:\\User_zhaoyingjun\\JavaSE\\Java_Eclipse_Workspace\\HelloWorld2\\src\\com\\zyjhandsome\\io\\RedirectIn.java");
12             // 将标准输入流 重定向到fis输入流
13             System.setIn(fis);
14             // 使用System.in创建Scanner对象,用于获取标准输入
15             Scanner sc = new Scanner(System.in);
16             // 增加下面一行只把回车作为分隔符
17             sc.useDelimiter("\n");
18             // 判读是否还有下一个输入项
19             while (sc.hasNext())
20             {
21                 // 输出输入项
22                 System.out.println("键盘输入的内容是:" + sc.next());                
23             }            
24         } catch (FileNotFoundException e) {
25             // TODO Auto-generated catch block
26             e.printStackTrace();
27         }
28     }
29 }

  输出结果:

 1 键盘输入的内容是:package com.zyjhandsome.io;
 2 
 3 键盘输入的内容是:
 4 
 5 键盘输入的内容是:import java.io.*;
 6 
 7 键盘输入的内容是:import java.util.*;
 8 
 9 键盘输入的内容是:
10 
11 键盘输入的内容是:public class RedirectIn {
12 
13 键盘输入的内容是:
14 
15 键盘输入的内容是:    public static void main(String[] args) {
16 
17 键盘输入的内容是:        // TODO Auto-generated method stub
18 
19 键盘输入的内容是:        try {
20 
21 键盘输入的内容是:            FileInputStream fis = new FileInputStream("D:\\User_zhaoyingjun\\JavaSE\\Java_Eclipse_Workspace\\HelloWorld2\\src\\com\\zyjhandsome\\io\\RedirectIn.java");
22 
23 键盘输入的内容是:            // 将标准输入流 重定向到fis输入流
24 
25 键盘输入的内容是:            System.setIn(fis);
26 
27 键盘输入的内容是:            // 使用System.in创建Scanner对象,用于获取标准输入
28 
29 键盘输入的内容是:            Scanner sc = new Scanner(System.in);
30 
31 键盘输入的内容是:            // 增加下面一行只把回车作为分隔符
32 
33 键盘输入的内容是:            sc.useDelimiter("\n");
34 
35 键盘输入的内容是:            // 判读是否还有下一个输入项
36 
37 键盘输入的内容是:            while (sc.hasNext())
38 
39 键盘输入的内容是:            {
40 
41 键盘输入的内容是:                // 输出输入项
42 
43 键盘输入的内容是:                System.out.println("键盘输入的内容是:" + sc.next());                
44 
45 键盘输入的内容是:            }            
46 
47 键盘输入的内容是:        } catch (FileNotFoundException e) {
48 
49 键盘输入的内容是:            // TODO Auto-generated catch block
50 
51 键盘输入的内容是:            e.printStackTrace();
52 
53 键盘输入的内容是:        }
54 
55 键盘输入的内容是:    }
56 
57 键盘输入的内容是:}

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

猜你喜欢

转载自www.cnblogs.com/zyjhandsome/p/9696794.html