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();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/chenmz1995/p/12262676.html