关于重定向这个概念我不太了解,下面是一段基础代码,请大家帮我说一下,这重定向有什么用?
import java.io.*;public class Redirecting {
  public static void main(String[] args)
  throws IOException {
    PrintStream console = System.out;
    BufferedInputStream in = new BufferedInputStream(
      new FileInputStream("Redirecting.java"));
    PrintStream out = new PrintStream(
      new BufferedOutputStream(
        new FileOutputStream("test.out")));
    System.setIn(in);  //这里是重定向
    System.setOut(out);   //这里是重定向
    System.setErr(out);   //这里是重定向
    BufferedReader br = new BufferedReader(
      new InputStreamReader(System.in));
    String s;
    while((s = br.readLine()) != null)
      System.out.println(s);
    out.close(); 
    System.setOut(console);   //这里为什么又改回来了?这么做有什么意义?
  }
}

解决方案 »

  1.   

    重定向,这个概念在很多语言中都有。比如:c++,Java。从你举的例子说,重定向指的是:将输出输入函数的默认输出位置做一个改变。例如:system.out 的默认输出位置为控制台(console),但现在它的输出位置为文件
      

  2.   

    最后一句,是一个好的编程习惯,不那样做也可以,但你必须保证在out.clost()后不用System.out,向控制台输出