如何将带有 5 个并行数组的输出 table 从主 class 打印到 .txt 文件?

How do I print my output table with 5 parallel arrays from my main class to a .txt file?

我正在尝试使用 class 打印(追加)到 .txt 文件。到目前为止,我一直没有成功。我正在使用一个简单的记录器 class 来输出简单的字符串,但是如何将输出输出到控制台以及 .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

有问题的主要 class 代码:

        //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?

好的,所以我找到了解决方法。它不是很漂亮,但现在就足够了。

我正在使用:

 PrintStream o = new PrintStream(new FileOutputStream("DashboardLog.txt", true));

然后

System.setOut(o);
System.setOut(console);

区分控制台输出和文件输出。

  try (PrintWriter out = new PrintWriter(new FileWriter("output.txt", true), true)) {
            out.write(message);
            out.close();}
catch(Exception e){
}
finally(){ 
            System.out.println(message);
}

?