FileOutputStream   fOutStream;   
PrintStream   pStream;   
try   
{   
            fOutStream   =   new   FileOutputStream(new   File(outname));   
            pStream   =   new   PrintStream(fOutStream);   
            System.setOut(pStream);
}catch(IOException   e)   
        {   
        } 
我现在用的这个方法,但是这样,output的所有东西都到那个outname的file里去了。
我想控制有的去outname,有的还是在控制台,可以和user继续互动,可以么? tyty!

解决方案 »

  1.   

    import   java.io.*; 
    public class Test8 
    {
    FileOutputStream fos;
    PrintStream ps; public void filePrint( String outName )
    {
    try
    {
    fos= new FileOutputStream( new File( outName ) );
    ps = new PrintStream( fos );
    //System.setOut( ps ); 
    }
    catch( IOException ioe )
    {

    }
    }

    public void consolePrint( String message )
    {
    System.out.println( message );
    }
    }

      

  2.   


    import   java.io.*; 
    public class Test8 
    {
    FileOutputStream fos;
    PrintStream ps; public void filePrint( String outName )
    {
    try
    {
    fos= new FileOutputStream( new File( outName ) );
    ps = new PrintStream( fos );
    //System.setOut( ps ); 
    }
    catch( IOException ioe )
    {

    }
    }

    public void consolePrint( String message )
    {
    System.out.println( message );
    }
    }
      

  3.   

    晕,发现有问题,楼上给的好像还是不work.
    我先用consolePrint,然后filePrint之后,就回不到consolePrint了。
    fos= new FileOutputStream( new File(outName) );
    ps = new PrintStream( fos );
    System.setOut( ps ); 
    这样是把output set成file.
    能有办法把他set回console么?
      

  4.   

    有办法了,那就在System.setOut(ps)之前,先记下目前的标准输出到哪里了.
    PrintStream old = System.out;
    这样如果想再设置回来就简单了:System.setOut(old);import java.io.*; 
    public class Test 
    {
    FileOutputStream fos;
    PrintStream ps;
            //for storing the old PrintStream
    PrintStream old; public void filePrint( String outName )
    {
    try
    {
    fos= new FileOutputStream( new File( outName ) );
    ps = new PrintStream( fos );
    //store the old PrintStream
    old = System.out;
    //set new PrintStream
    System.setOut( ps ); 
    }
    catch( IOException ioe )
    {

    }
    finally
    {
    try
    {
    ps.close();
    fos.close();
    }
    catch( IOException ioe )
    {

    }
    }
    }

    public void consolePrint( String message, PrintStream old )
    {
    System.setOut( old );
    System.out.println( message );
    }
    }