/**標準IOjava.lang.System類中提供以下三個靜態常量:staticfinalInputStreamin功能:“標準”輸入流,流已打開并準備提供輸入數據。通常,此流對應于鍵盤輸入或者由主機環境或用戶指...
/**
標準IO
java.lang.System類中提供以下三個靜態常量:
static final InputStream in
功能:“標準”輸入流,流已打開并準備提供輸入數據。通常,此流對應于鍵盤輸入或者由主機環境或用戶指定的另一個輸入源。
static final PrintStream out
功能:“標準”輸出流,此流已打開并準備接受輸出數據。通常,此流對應于顯示器輸出或者由主機環境或用戶指定的另一個輸出目標。
static final PrimeStream err
功能:“標準”錯誤輸出流。此流已打開并準備接受輸出數據。通常,此流對應于顯示器輸出或者由主機環境或者用戶指定的另一個輸出目標。
以上3種流都是java虛擬機在啟動應用程序自動創建的。
static void setIn(InputStream in):對標準輸入流重定向
static void setOut(PrintStream out):對標準輸出流重定向
static void serErr(PrintStream err):對標準錯誤輸出流重定向
*/
import java.io.*;
public class StandardIORedirect{
public static void main(String[] args)throws IOException{
PrintStream console = System.out;
BufferedInputStream in = new BufferedInputStream(new FileInputStream
("E:\\JavaPractice\\StandardIORedirect.java"));
PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream
("E:\\JavaPractice\\StandardIORedirect.txt")));
System.setIn(in); //對輸出流重定向
System.setOut(out);
System.setErr(out);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
while((s=br.readLine())!=null){ //從BufferedReader類中讀取一行數據
System.out.println(s);
}
out.close();
System.setOut(console);
}
}
in和out是否要關閉?
展開