1. 程式人生 > >Java 輸入/輸出——重定向標準輸入/輸出

Java 輸入/輸出——重定向標準輸入/輸出

ole catch som align oid int limit 文件的 pan

  在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的構造器:

ConstructorDescription
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文件作為標準輸入源。

Java 輸入/輸出——重定向標準輸入/輸出