Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31886
| Newsgroups | comp.lang.java.programmer |
|---|---|
| Date | 2015-06-30 05:45 -0700 |
| References | <635be647-55fb-43f9-8609-0320306ca853@googlegroups.com> |
| Message-ID | <450dd941-b331-4f46-9e9c-e7db4033e0ca@googlegroups.com> (permalink) |
| Subject | Re: basic output question |
| From | e.d.programmer@gmail.com |
On Monday, June 29, 2015 at 2:40:05 PM UTC-4, Russ P. wrote:
> Suppose I need a function (or method) for printing some (text-based) information to a file or to standard output. What is the recommended way to do this while minimizing duplication of code? Here's a simplification of what I came up with (in Scala, but using the Java standard library):
>
> def printInformation(out: java.io.PrintStream = System.out) {
>
> // print information here
> }
>
> def printInformation(fileName: String) {
> printInformation(new java.io.PrintStream(fileName))
> }
>
> So if I call printInformation with no arguments, the output goes to standard output, but if I give it a filename the output goes to the file. Is this the best way to do it, or is there a simpler/better way? I'd also like to be able to append the output to an existing file as well as create a new file. Thanks.
I never quite got the logging frameworks ie commons logging. You have to deal with someone else's API, set warning levels, and include an extra jar in the classpath. I wrote my own logging class since System.out.println() by itself is a bit too basic, and you don't want to write more than one line each time you print a log message. My logging class does just use System.out.println() but with overloads to print a String, a number, or an Object. All overloads call a common method which prints the message with a timestamp and gets the method that printed the message from the StackTrace. The Object method has the condition to check if it's an array to print the value of each element. Writing to a file I use another class, since as you say we want to avoid duplication. You may want to print log messages to a file but there are a lot of other things you may want to write to a file.
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
basic output question "Russ P." <russ.paielli@gmail.com> - 2015-06-29 11:39 -0700
Re: basic output question Arne Vajhøj <arne@vajhoej.dk> - 2015-06-29 14:53 -0400
Re: basic output question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-29 15:32 -0400
Re: basic output question e.d.programmer@gmail.com - 2015-06-30 05:45 -0700
Re: basic output question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-30 09:05 -0400
Re: basic output question e.d.programmer@gmail.com - 2015-06-30 11:28 -0700
Re: basic output question Arne Vajhøj <arne@vajhoej.dk> - 2015-06-30 14:55 -0400
Re: basic output question e.d.programmer@gmail.com - 2015-06-30 12:14 -0700
Re: basic output question Arne Vajhøj <arne@vajhoej.dk> - 2015-07-27 22:25 -0400
Re: basic output question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-30 15:27 -0400
csiph-web