簡體   English   中英

如何將包含 5 個並行數組的輸出表從主類打印到 .txt 文件?

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

我正在嘗試使用一個類來打印(附加)到 .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?

好的,所以我找到了解決方法。 它不漂亮,但現在就足夠了。

我在用:

 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);
}

?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM