Write the output of the Java program to a txt file

1. Output the output results to a txt file

Step 1: Header file import java.io.FileOutputStream;  
                             import java.io.PrintStream; 

               It may also be reminded to throw an error, so import java.io.FileNotFoundException;

Step 2:

   PrintStream ps = new PrintStream("e:/log.txt");  search// Create a print output stream, the output target is: the log.txt file of the E drive

Step 3:

   System.setOut(ps);//Assign the created printout stream to the system. That is, the system will output to ps next time

   System.out.println("This line will be written to the log.txt file");

2. Re-output the output to the screen

step one

Step 2

Step 3:

PrintStream out = System.out; // First cache the system's default print output stream

System.out.println("This line is relocated to standard output - screen");

          

           



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325684898&siteId=291194637