一个简单的java重定向程序,为什么要恢复System . out 对象,却不要恢复System . in对象
PHPz
PHPz 2017-04-17 17:29:47
0
1
232
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) ;
        } 
}
PHPz
PHPz

学习是最好的投资!

모든 응답(1)
伊谢尔伦

System.out을 복원하지 않고 어떤 일이 일어나는지 확인할 수도 있습니다.
이론적으로는 프로그램이 종료되어 복원 여부는 중요하지 않습니다.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿