I am trying to employ a class to print (append) to a .txt file. I have been unsuccessful so far. I am using a simple logger class to output simple Strings, but how can a output to console and also a .txt file?
Current console output:
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
Main class code in question:
//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?");
Logger code:
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();
Current file output:
How do I get my table to print here?
Ok so I found a work around. It is not pretty but will suffice for now.
I am using:
PrintStream o = new PrintStream(new FileOutputStream("DashboardLog.txt", true));
And then
System.setOut(o);
System.setOut(console);
To separate between console output and file output.