$ command >outfile
這個(gè)用法是說(shuō):運行一個(gè)命令,并將它的標準輸出 (例如System.out.println的輸出)定向到指定的文件而不是輸出到控制臺或者屏幕。
這個(gè)特性非常有用。在java程序中也可以達到這個(gè)目的而不用依賴(lài)與shell。通常情況下如果你使用的編程風(fēng)格依賴(lài)于標準輸入輸出 (就像UNIX shell 和其它工具包那樣), 你可能不需要或者不想在程序里面重定向。但是有時(shí)候你希望這樣,讓我們看幾個(gè)范例。
import java.io.*;
public class RedirectDemo1 {
public static void main(String args[]) throws IOException {
// 在文件上建立一個(gè)PrintStream
FileOutputStream fos = new FileOutputStream("out.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
PrintStream ps = new PrintStream(bos, false);
// 重定向System.out 到該文件
System.setOut(ps);
// 輸出
System.out.println("This is a test\u4321");
int n = 37;
System.out.println("The square of " + n + " is " + (n * n));
ps.close();
}
}
標準輸出 (System.out) 是PrintStream 對象的一個(gè)引用。為了重定向輸出,創(chuàng )建一個(gè)這樣的對象來(lái)表示一個(gè)文件或者其它的輸出流 (例如一個(gè)網(wǎng)絡(luò )連接)。然后使用System.setOut 調用改變System.out 引用。
JDK 1.3 中實(shí)現的java.lang.System 代碼是這樣的形式:
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
setOut0(new PrintStream( new BufferedOutputStream(fdOut, 128), true));
這個(gè)代碼初始化System.out。因此,在缺省情況下,System.out 是一個(gè)PrintStream,該PrintStream 是從FileDescriptor.out 創(chuàng )建的一個(gè)FileOutputStream 文件,帶有128字節的緩沖區,自動(dòng)刷新(遇到新行符或者字節向量時(shí)自動(dòng)輸出)。當輸出像上面那樣被重定向,System.out 變成使用System.setOut傳遞進(jìn)來(lái)的一個(gè)新創(chuàng )建的PrintStream 對象的引用。
RedirectDemo1 有幾個(gè)問(wèn)題,首先是PrintStream 使用平臺缺省的字符編碼將字符轉換為字節。這通常是好事,例如,如果你的小程序中有這樣一行:
System.out.println("this is a test");
而且你在美國這樣運行程序:
$ java prog >outfile
程序在文件"outfile"中存入ASCII字符。這可能是你想要的得到的7位的ASCII文本文件。
但是程序中如果有Unicode字符‘\u4321‘,它將被轉換為一個(gè)‘?‘ 。如果你查看文件你可以看到?。
換句話(huà)說(shuō),缺省的編碼沒(méi)有正確的處理那個(gè)輸出字符。
另一個(gè)問(wèn)題是I/O 重定向可以被推廣,例如你能將輸出定向到一個(gè)字符串而不是一個(gè)文件。
這里有一個(gè)范例包括了上面的兩個(gè)問(wèn)題:
import java.io.*;
public class RedirectDemo2 {
public static void main(String args[]) throws IOException {
// 在StringWriter 上建立一個(gè)PrintWriter。
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
// 輸出一些東西到StringWriter
pw.println("This is a test\u4321");
int n = 37;
pw.println("The square of " + n + " is " + (n * n));
// 得到被寫(xiě)入的字符串
String str = sw.toString();
// 顯式它
System.out.print(str);
// 將字符串輸出到文件,使用 UTF-8 編碼
FileOutputStream fos = new FileOutputStream("out.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write(str);
bw.close();
// 讀回字符串并檢查
FileInputStream fis = new FileInputStream("out.txt");
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String s1 = br.readLine();
String s2 = br.readLine();
br.close();
String linesep = System.getProperty("line.separator");
if (!str.equals(s1 + linesep + s2 + linesep))
System.err.println("equals check failed");
}
}
范例的第一部分在一個(gè)StringWriter 上建立一個(gè)PrintWriter對象,它和PrintStream 類(lèi)似,但是操作的是字符而不是字節流。StringWriter 用于向一個(gè)動(dòng)態(tài)的內部緩沖區聚集字符,以后恢復為一個(gè)String或者StringBuffer。
在輸出被寫(xiě)入StringWriter 后,聚集的字符串恢復了,然后字符串使用OutputStreamWriter和UTF-8編碼被寫(xiě)入文件。這個(gè)編碼在所有的java實(shí)現中都被支持。它將‘\u0001‘ 到 ‘\u007f‘范圍內的字符編碼為一個(gè)字節而其它字符為兩個(gè)或者三個(gè)字節。
最后,字符串從文件讀回,還是使用UTF-8 編碼。然后和原始的字符串比較。原始的字符串內有兩個(gè)行分隔符,因此讀回的是兩個(gè)字符串,為了比較而添加了行分隔符。
注意你也可以從一個(gè)文件或者字符串重定向輸入,使用一個(gè)像StringReader這樣的類(lèi)。
聯(lián)系客服