0

I have just over 100 lines of data and statistics that I need to print. I'm using:

PrintWriter p = new PrintWriter(fileName);

The reason why it's about 100 lines is because I need to print to both the System and the file (about 50 lines each). Is there any shorter way to print and "prettify" up my code?

System.out.println("Prints Stats"); //To System
p.println("Prints Stats"); //To File
etc

For every line printed to the System, there is an exact same line that is printed to the file. Is there a way to combine the two or to make it shorter? Or am I just stuck with this "ugly", long pile of prints?

MrAwesome8
  • 269
  • 1
  • 2
  • 9
  • Possible duplicate: [Writing to console and text file](http://stackoverflow.com/questions/16237546/writing-to-console-and-text-file) – Pshemo Nov 17 '14 at 19:18
  • You could use a [Logger](https://docs.oracle.com/javase/7/docs/api/java/util/logging/Logger.html)... – Mena Nov 17 '14 at 19:19
  • Everything works fine Pshemo, I just want to find a way to concise it. – MrAwesome8 Nov 17 '14 at 19:20
  • 1
    I know. Accepted answer shows one of ways to set up your code so you could just use `System.out.println` and have it printed in console and in file (so you wouldn't need additional lines which would invoke `p.println`). – Pshemo Nov 17 '14 at 19:22
  • Okay, thank you, I got that working. Now, is there a way to only print to the console now that I have done this? – MrAwesome8 Nov 17 '14 at 19:42

1 Answers1

1

There are several ways to do this.

Using a StringBuilder

If you are not writing tons of text, you could use a StringBuilder to create your output by appending to it, inserting stuff inside it etc., and once it's ready, print it to both p and System.out.

StringBuilder b = new StringBuilder();
b.append("Name: ").append("Susan").append("\n");
// Append more stuff to b, also insert and delete from it if you want.
System.out.print(b);
p.print(b);

Writing your own println()

If you're using just the println() method, you could write your own method that calls it for both writers:

private void println( String s ) {
    System.out.println(s);
    p.out.println(s);
}

That is, assuming p is a field and not a local variable.

Using format

Instead of using println() you could use the printf() or format() methods. The first parameter is a formatting string, and you can format several lines within one print using a format string. For example:

System.out.printf( "Name: %s%nSurname: %s%nAge: %d%n", "Susan", "Carter", 30 );

Would print

Name: Susan
Surname: Carter
Age: 30

And by using the same format string you can use two printfs to save on many printlns:

String formatString = "Name: %s%nSurname: %s%nAge: %d%n";
Object[] arguments = { "Susan", "Carter", 30 );

p.printf( formatString, arguments );
System.out.printf( formatString, arguments );

This would print the above three-line output to your file and then to your System output.

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • Thanks, I wrote my own print method (called it printlnf) that way it can clear up a lot of code in my most convenient way, and it also accommodates for my need of also using console output that doesn't get directed to the file. Thanks! – MrAwesome8 Nov 17 '14 at 20:44