public class Redirectoutputstream {
public static void Main (string[] args) {
try {
PrintStream out = System.out; Save the original output stream
PrintStream PS = new PrintStream ("/log.txt"); Creating a file output stream
System.setout (PS); Set up to use the new output stream
int age = 18; Defining Shaping variables
SYSTEM.OUT.PRINTLN ("Age variable is defined successfully, initial value is 18");
String sex = "female"; Defining string variables
SYSTEM.OUT.PRINTLN ("The gender variable is defined successfully, the initial value is female");
Consolidation of two variables
String info = "This is a" +sex+ "child, should have" +age+ "years old. ";
System.out.println ("consolidates two variables as info string variable, the result is:" +info);
System.setout (out); Restore the original output stream
SYSTEM.OUT.PRINTLN ("The program has finished running, please review the log file. ");
} catch (Exception e) {
E.printstacktrace ();
}
}
}
/**
* The SetOut () method of the system class in this example changes the output stream
The out, err, and in member variables of the *system class are of the final type and cannot be directly assigned, and the flow is changed by the corresponding method
* 1, SetOut () method
* This method is used to reassign the standard output stream of the system class, public static void SetOut (PrintStream out)
* Parameter Description
*out: New PrintStream output Stream object
* 2, Seterr () method
* The method will reassign the standard error output stream of the system class public static void SetIn (InputStream in)
* Parameter Description
*in: New InputStream Input Stream object
*/
This article is from the "Java" blog, so be sure to keep this source http://4534508.blog.51cto.com/4524508/1961891
Java Redirection output stream implementation program log