JAVA I/O重定向

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class Redirecting {

	public static void main(String[] args) throws IOException {
		PrintStream console = System.out;
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File("src/com/io/Redirecting.java").getAbsolutePath()));
		PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream(new File("src/com/io/").getAbsolutePath() +"/test.out")),true);//第二个参数很重要
		System.setIn(in);
		System.setOut(out);
		System.setErr(out);
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		String s;
		while((s = reader.readLine())!= null){
			System.out.println(s);
			out.close();
			System.setOut(console);
		}
	}
}



posted @ 2012-04-14 23:04  张长胜  阅读(196)  评论(0编辑  收藏  举报