我试图使用一个类来打印(追加)到一个.txt文件。到目前为止,我一直没有成功。我使用一个简单的记录器类来输出简单的字符串,但是如何才能输出到控制台和一个.txt文件呢?
当前控制台输出:
11/18/20 14:09:24
Current status of all items being tracked:
Item| Supply on hand| Last 24 Hr Usage| Days on hand| Status|
-------------------------------------------------------------------------------------
1 1 1 1 Critical
Process finished with exit code 0所涉主要类代码:
//Display all data, collected and calculated
System.out.println("Current status of all items being tracked:");
System.out.printf("%-16s %16s %16s %16s %16s", "Item|", "Supply on hand|", "Last 24 Hr Usage|", "Days on hand|", "Status|");
System.out.println();
System.out.println("-------------------------------------------------------------------------------------");
System.out.println();
for (int index = 0; index < items.length; index++)
System.out.printf("%-16s %16d %16d %16d %16s \n", items[index], supplyOnHand[index], last24HourUsage[index], daysOnHand[index], status[index]);
Logger.log("How do I get my table to print here?");记录器代码:
public class Logger {
public static void log(String message) throws IOException {
try (PrintWriter out = new PrintWriter(new FileWriter("output.txt", true), true)) {
out.write(message);
out.close();当前文件输出:
How do I get my table to print here?发布于 2020-11-18 20:20:07
好吧,所以我在附近找到了一份工作。这不是很漂亮,但现在已经足够了。
我正在使用:
PrintStream o = new PrintStream(new FileOutputStream("DashboardLog.txt", true));然后
System.setOut(o);
System.setOut(console);若要在控制台输出和文件输出之间进行分隔,请执行以下操作。
发布于 2020-11-18 20:43:10
try (PrintWriter out = new PrintWriter(new FileWriter("output.txt", true), true)) {
out.write(message);
out.close();}
catch(Exception e){
}
finally(){
System.out.println(message);
}https://stackoverflow.com/questions/64899862
复制相似问题