[英]How to format and save console output to file in Java?
背景:您好! 首先,请记住,我是Java的新手,因此,如果我遇到问题,请原谅我。 我一直在尝试格式化控制台并将其输出保存到文本文件中已有2天了。 到目前为止,这是我的代码:
我正在尝试将控制台的格式设置为
[12/20/2017 23:55:30]程序(此处的消息)
我这样做是这样的:
public class Format {
private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
PrintStream console = new PrintStream(System.out) {
public void println(String x) {
Date date = new Date();
super.println("[" + sdf.format(date) + "] " + "program(" + x + ")");
}
};
伴随着这个:
public void progStream() throws FileNotFoundException {
System.setOut(console);
System.out.println("This is a test.");
}
到这里为止一切正常,我可以看到所有系统输出都以正确的格式显示在Eclipse的控制台上。 但是,当我尝试将其保存为文本文件时,它仅保存消息。 没有格式。
public void progStream() throws FileNotFoundException {
File log = new File("log" + System.currentTimeMillis() + ".txt");
FileOutputStream fos = new FileOutputStream(log);
PrintStream console = new PrintStream(fos);
System.setOut(console);
System.out.println("This is a test.");
}
我尝试替换System.out.println("message");
使用console.print("message");
但我遇到同样的问题。 不幸的是,我已经没有足够的教程和论坛来查找了,但仍然找不到解决方案。 谢谢您的帮助。
在代码中,您永远不会使用创建的Format类在输出之前加上日期。 您应该已经创建了此类的实例,并调用了新创建实例的console
字段的方法println()
。 下面是经过稍微修改的代码,我想可以完成您想要的工作:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class So_47900381 {
static public class Format {
private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
PrintStream console = new PrintStream(System.out) {;
public void println(String x) {
Date date = new Date();
super.println("[" + sdf.format(date) + "] " + "program(" + x + ")");
}
};
}
public static void progStream() throws FileNotFoundException {
File log = new File("log" + System.currentTimeMillis() + ".txt");
PrintStream console = new PrintStream(new FileOutputStream(log));
System.out.println("Writing to " + log.getPath());
System.setOut(console);
Format fmt = new Format();
fmt.console.println("This is a test.");
}
public static void main(String[] args) {
try {
progStream();
} catch (Exception x) { x.printStackTrace(); }
}
}
但是我认为不需要在Format
类内部有一个单独的字段,它是PrintStream
的后代并用于打印到该字段。 该类本身也可以从PrintStream
。 看下面的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class So_47900381_1{
static public class Format extends PrintStream {
private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
public Format(File file) throws FileNotFoundException {
super(file);
System.setOut(this);
}
public void println(String x) {
Date date = new Date();
super.println("[" + sdf.format(date) + "] " + "program(" + x + ")");
}
}
public static void progStream() throws FileNotFoundException {
File log = new File("log" + System.currentTimeMillis() + ".txt");
System.out.println("Writing to " + log.getPath());
Format fmt = new Format(log); // redirects the output to the file
System.out.println("This is a test."); // now it's written to the file
}
public static void main(String[] args) {
try {
progStream();
} catch (Exception x) { x.printStackTrace(); }
}
}
是的,请查看log4j
和/或Google进行java logging
。
我认为这就是您想要实现的目标:
import java.lang.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class HelloWorld {
public static void main(String[] args) throws IOException {
PrintStream console = new PrintStream(System.out);
PrintStream file = new PrintStream(new FileOutputStream(new File("log" + System.currentTimeMillis() + ".txt")));
Format format = new Format();
// console
format.print("bla", console);
// file
format.print("bla", file);
}
}
public class Format {
private static final DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
public void print(String x, PrintStream stream) {
Date date = new Date();
stream.println("[" + sdf.format(date) + "] " + "program(" + x + ")");
}
}
PS .:如果您真的想记录日志,建议您考虑使用log4j之类的日志记录框架。
PS。 2: System.setOut(stream)
似乎不是一个好主意,因为您的标准输出将被重定向到文件。
希望我的回答对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.