redirectexample.java

来自「java 完全探索的随书源码」· Java 代码 · 共 42 行

JAVA
42
字号
import java.io.*;import java.sql.Timestamp;class RedirectExample{  // Change this path if you are using unix or want another path  public static final String FILE = "c:\\systemin.txt";  public static void main(String args[])  {    try    {      // Create a file output stream and set the append = true flag      FileOutputStream outStr = new FileOutputStream( FILE, true );      // create a print stream so that we can connect to System.out      PrintStream printStream  = new PrintStream( outStr );      // Tell System.out to redirect to the printStream that we've created      System.setOut( printStream );      // Print out some text. It should not go to the console, but to the      // text file created above.      Timestamp now = new Timestamp( System.currentTimeMillis() );      System.out.println( now.toString() + ": This is text that should go to the file" );      // Close the open Resources      outStr.close();      printStream.close();    }    catch( FileNotFoundException ex )    {      ex.printStackTrace();      System.exit( -1 );    }    catch( IOException ex )    {      ex.printStackTrace();      System.exit( -1 );    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?