Java 如何将带有5个并行数组的输出表从主类打印到.txt文件?
我试图使用一个类来打印(附加)到一个.txt文件。到目前为止我一直不成功。我使用一个简单的logger类来输出简单的字符串,但是如何将输出输出到控制台和.txt文件呢 当前控制台输出:Java 如何将带有5个并行数组的输出表从主类打印到.txt文件?,java,file,filewriter,write,txt,Java,File,Filewriter,Write,Txt,我试图使用一个类来打印(附加)到一个.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| ----------
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
How do I get my table to print here?
有关的主要类别代码:
//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?");
当前文件输出:
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
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);
}
?将控制台中的所有输出放在一个字符串数组中,并将其打印到文件中。听起来我应该知道怎么做,但您能详细说明一下吗?如何将输出放在字符串数组上?我想我得到了。谢谢你的帮助!