1 PackageHahaa;2 3 Importjava.io.FileNotFoundException;4 ImportJava.io.PrintStream;5 6 Public classha {7 Public Static voidMain (string[] args) {8 Try { 9PrintStream out =System.out; Save the original output streamTenPrintStream PS =NewPrintStream ("./log.txt");//create file output stream One A System.setout (PS); Setting up a stream with a new file output - intAge = 11; -SYSTEM.OUT.PRINTLN ("Age variable is defined successfully, initial value is 11"); theString sex = "female"; -SYSTEM.OUT.PRINTLN ("Age variable successfully defined, initial value is female"); - //consolidation of 2 variables -String info = "This is a" + Sex + "child, should have" + age + "years old"; +System.out.println ("consolidates two variables as info characters, the result is:" +info); - System.setout (out); Back to the console again . +SYSTEM.OUT.PRINTLN ("The program has finished running, please check the log"); A}Catch(FileNotFoundException e) { at E.printstacktrace (); - } - } -}
Java Self-study note-Basic syntax 3setOut () method to set a new output stream