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

zzzyeukh  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(353)

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

lrpiutwd1#

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

?

suzh9iv8

suzh9iv82#

好吧,我找到了一份工作。虽然不漂亮,但现在就够了。
我正在使用:

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

然后

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

将控制台输出和文件输出分开。

相关问题