[英]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.