Java I/O学习 标准的I/O重定向

public class Test{

	/*
	 * 标准的I/O重定向
	 * System.setIn(InputStream)
	 * System.setOut(PrintStream)
	 * System.setErr(PrintStream)
	 */
	public static void main(String[] args) throws IOException {
		PrintStream console = System.out;
		BufferedInputStream in = new BufferedInputStream(new FileInputStream("/home/estar/Test/a.java"));
		PrintStream out = new PrintStream("and.out");
		
		System.setIn(in);
		System.setOut(out);
		System.setErr(out);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s = null;
		while ((s = br.readLine()) != null) {
			System.out.println(s);
		}
		out.close();
		System.setOut(console);
		
	}
}

  

原文地址:https://www.cnblogs.com/E-star/p/3443331.html