猿问

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

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) ;

        } 

}


阿晨1998
浏览 387回答 1
1回答

POPMUISE

你也可以试试不恢复 System.out 看会咋个样。理论上来说,程序都结束了,恢复不恢复都没关系了。
随时随地看视频慕课网APP

相关分类

Java
我要回答