上代码了解一下:
1 package com.etc; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.InputStream; 9 import java.io.PrintStream; 10 import java.util.Scanner; 11 12 /* 13 * 对输入输出流进行打印 14 * System.in 15 * System.out 16 * System.err 17 * 输入输出重定向: 18 * System.setIn(InputStream in) 19 * System.setOut(PrintStream out) 20 * System.setErr(PrintStream err); 21 * 回控制台: 22 * FileDiscriptor.in 23 * FileDiscriptor.out 24 * FileDiscriptor.err 25 */ 26 public class OverideSystem { 27 28 public static void main(String[] args) throws FileNotFoundException { 29 Overide(); 30 System.out.println("I like codes very much!"); 31 testPrintStream(); 32 } 33 //重定向方法实现 34 public static void Overide() throws FileNotFoundException { 35 //重定向输入方式及自动更新 36 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("E:/test.txt")),true)); 37 } 38 39 public static void testPrintStream() throws FileNotFoundException { 40 //通过文件输入流放入系统的输入,然后再通过Scanner函数将输入结果打印出来 41 InputStream in=System.in; 42 in=new BufferedInputStream(new FileInputStream("E:/test.txt")); 43 Scanner sc=new Scanner(in); 44 System.out.println("输出结果:"); 45 System.out.println(sc.nextLine()); 46 47 } 48 }
效果截图:
ps:文章仅作学习了解一用,并未深入,欢迎大佬点评。
原文地址:https://www.cnblogs.com/weekstart/p/10818570.html
时间: 2024-10-09 13:27:47