検索

ホームページ  >  に質問  >  本文

一个简单的java重定向程序,为什么要恢复System . out 对象,却不要恢复System . in对象

import java . io .* ;

public class StandardIO
{
        public static void main (String [ ] args) throws IOException
        {
                
                PrintStream console = System . out ;
                // InputStream stdin = System .in ;         // 为什么不需要恢复该对象
                BufferedInputStream in = new BufferedInputStream ( 
                        new FileInputStream ("StandardIO.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) ;
        } 
}
PHPzPHPz2887日前241

全員に返信(1)返信します

  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:31:47

    System.out を復元しないで、何が起こるかを試してみることもできます。
    理論的には、プログラムは終了しているため、復元するかどうかは問題ではありません。

    返事
    0
  • キャンセル返事