Simpler way to write a java output to both console and text file?

12,374

Solution 1

Commons IO has a TeeOutputStream that writes everything to two outputs at once.

Solution 2

The simplest in your case would be to create an output method:

private void output(final String msg, PrintStream out1, PrintWriter out2) {        
    out1.println(msg);
    out2.println(msg);
}

And using it like this:

output("your message", System.out, outputfile);

Solution 3

First of all, if it's important that the formatting be identical between console and file, then I'd save all of your output to a single String, embedding the '\n' characters and such, and then just print/write that String all in one go.

I don't know off hand whether or not there's a method for writing to both file and console at the same time. You can always write a function that will do both of these things though.

Solution 4

Log4j is a Reliable, Fast and Flexible Logging Framework for console and file appending.

Logging Example to append on Console and File togather!

Share:
12,374
DarkD
Author by

DarkD

Updated on August 27, 2022

Comments

  • DarkD
    DarkD over 1 year

    So im a pretty big noob at java and I have a exam coming up and have been practicing all week. My progress has been slow but steady. One exercise I am doing requires me to print the output of an array calculation to a console AND create a file temperature.txt with the console output and same formatting.

    I've figured out how to be able to do one or the other but i'm having trouble doing both (remember slow and steady learner). Is there a simpler beginners method to keep in mind?

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.DecimalFormat;
    
    public class Temperature {
    
        public static void main(String[] args) throws IOException{ 
            double[] temps = {100, 98, 80.5, 90.2, 99.4, 89.1};
            MaxMin(temps);
    
        }
    
        public static void MaxMin(double[]temps) throws IOException{
            FileWriter fwriter = new FileWriter("Temperatures.txt", true);
            PrintWriter outputfile = new PrintWriter(fwriter);
            double min= Double.MAX_VALUE;
            double max= Double.MIN_VALUE;
            double sum = 0;
    
            DecimalFormat formatter = new DecimalFormat("0.0");
    
            for(int i = 0;i<temps.length;i++){
                sum += temps[i];
                if(temps[i] > max){
                    max = temps[i];
                }
                if(temps[i]< min){
                    min = temps[i];
                }
            }
    
            double avg = sum / temps.length;
            outputfile.println("Average Temp: " + formatter.format(avg));
            outputfile.println("Maximum Temp: " + formatter.format(max));
            outputfile.println("Minimum Temp: " + formatter.format(min));
            outputfile.close();
        }
    }
    

    What I have above prints the text but nothing on the console.

    edit: would it be acceptable to just add 3 System.out.println statements at the end to fulfill both requirements? like this:

    System.out.println("Average Temp: " + formatter.format(avg));
    System.out.println("Maximum Temp: " + formatter.format(max));
    System.out.println("Minimum Temp: " + formatter.format(min));
    
    outputfile.println("Average Temp: " + formatter.format(avg));
    outputfile.println("Maximum Temp: " + formatter.format(max));
    outputfile.println("Minimum Temp: " + formatter.format(min));
    outputfile.close();