背景:您好!首先,请记住,我对 Java 还很陌生,因此,如果我有什么问题,请原谅我。我已经尝试格式化我的控制台并将其输出保存到文本文件两天了。到目前为止,这是我的代码:
我正在尝试将控制台的格式设置为
[12/20/2017 23:55:30] program(message here)
我这样做:
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(); }
}
}
但我认为没有必要有一个单独的字段,它是 PrintStream
的后代并用于打印到 Format
类内。该类本身也可以从 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 日志记录
。
关于java - 如何在 Java 中格式化控制台输出并将其保存到文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47900381/