java System.out.println 输出重定向到文件中

 

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;

public class SystemOutTest {
    public static void main(String[] args) throws Exception {
        String property = System.getProperties().getProperty("user.home");
        String filePath = property.replace("\\", "/");
        String fileName = filePath + "/out.txt";
        try {
            System.setOut(new PrintStream(new FileOutputStream(fileName)));
            while (true) {
                Thread.sleep(5000);
                System.out.println(new Date());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2020-02-05 10:26  护花使者  Views(1523)  Comments(0Edit  收藏  举报