Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31886

Re: basic output question

X-Received by 10.66.152.171 with SMTP id uz11mr26157205pab.10.1435668321846; Tue, 30 Jun 2015 05:45:21 -0700 (PDT)
X-Received by 10.140.93.38 with SMTP id c35mr21075qge.38.1435668321797; Tue, 30 Jun 2015 05:45:21 -0700 (PDT)
Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!j8no3252912igd.0!news-out.google.com!4ni30758qgh.1!nntp.google.com!j5no1540041qga.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.programmer
Date Tue, 30 Jun 2015 05:45:21 -0700 (PDT)
In-Reply-To <635be647-55fb-43f9-8609-0320306ca853@googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H
NNTP-Posting-Host 50.78.95.177
References <635be647-55fb-43f9-8609-0320306ca853@googlegroups.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <450dd941-b331-4f46-9e9c-e7db4033e0ca@googlegroups.com> (permalink)
Subject Re: basic output question
From e.d.programmer@gmail.com
Injection-Date Tue, 30 Jun 2015 12:45:21 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.java.programmer:31886

Show key headers only | View raw


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


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