Java forum for Java programmers

How to read back the lines printed out to the console?

Jan 30, 2012 8:03 pm
Frame

Hi:

We have an existing Java program, which prints out a lot of message
using System.out.println() method to the console. There are about 500
those calls in the code. We hope to add one more feature: besides
print out to the console as it is doing now, we also want to store
those messages in a text file. So the message will be in two places:
one place -- the console -- is shown the message progressively as the
program is running; another place -- a text file -- is created at the
end of the program.

Since there are about 500 calls in the code, we don't want to add a
duplicated printing method at every printing place. I am thinking to
let the program run as usal, printing out all the messages to the
console, then before the program ends, having a method reading in
every line on the console, which was printed out previously. I just
don't know how to achieve that.

Thank you very much.

Jan 30, 2012 8:14 pm
Stefan Ram
Re: how to read back the lines printed out to the console?

frame <xsli2@yahoo.com> writes:
>Since there are about 500 calls in the code, we don't want to add a
>duplicated printing method at every printing place. I am thinking to

http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#setOut(java.io.PrintStream)



Jan 30, 2012 8:18 pm
Glen herrmannsfeldt
Re: how to read back the lines printed out to the console?

frame <xsli2@yahoo.com> wrote:

> We have an existing Java program, which prints out a lot of message
> using System.out.println() method to the console. There are about 500
> those calls in the code. We hope to add one more feature: besides
> print out to the console as it is doing now, we also want to store
> those messages in a text file. So the message will be in two places:
> one place -- the console -- is shown the message progressively as the
> program is running; another place -- a text file -- is created at the
> end of the program.

The old-fashioned way on unix is the tee command, which you can pipe
into, and will write to a file and stdout.

It should be easy to port to other systems with pipe, such as Windows.

-- glen


Jan 30, 2012 10:18 pm
Daniel Pitts
Re: how to read back the lines printed out to the console?

On 1/30/12 12:03 PM, frame wrote:
> Hi:
>
> We have an existing Java program, which prints out a lot of message
> using System.out.println() method to the console. There are about 500
> those calls in the code. We hope to add one more feature: besides
> print out to the console as it is doing now, we also want to store
> those messages in a text file. So the message will be in two places:
> one place -- the console -- is shown the message progressively as the
> program is running; another place -- a text file -- is created at the
> end of the program.
>
> Since there are about 500 calls in the code, we don't want to add a
> duplicated printing method at every printing place. I am thinking to
> let the program run as usal, printing out all the messages to the
> console, then before the program ends, having a method reading in
> every line on the console, which was printed out previously. I just
> don't know how to achieve that.
>
> Thank you very much.
>
Perhaps System.out isn't the way to go. You might want to look into
logging frameworks, such as commons logging or log4j. They provide that
kind of flexibility and more.




Previous Thread: Resource confusion
Next Thread: Java DB rotation

Related Forum Topics
Inserting Multiple Lines from Console
Dear Group,

I was trying to get input of different nature of string, int etc.

To insert multiple lines I can do sort of standard coding like,

public class BRReadLines {
public static void main(String args[]) throws IOException
{
// Create a BufferedReader using System.in
...
Read DEL and BACK key from stdin
Is it possible to read DEL and BACKSPACE keys from standard in?

If I use:

System.in.read()

these keys are never returned.

Thanks.


Back to .Net? lesser of two evils?
With Oracle on one side suing Google over Android
and on the other cooing about how many phones
run Java; I'm wondering if it might be worth it
to put my ideological views about Microsoft aside
and consider going back to .Net.

--
// This is my opinion.


Ant task to compute lines of code
Hello,

I want to find out the total lines of code of a project using an ant task.

On the commandline I would do this like this:

find src -name *.java | wc -l

In ant, I only managed to output all the code:

<target name="loc">
<exec executable="find">
<arg line="src -name...
Input from Console
Dear Group,

I am trying to learn Java. I was trying to write a code for input from Console.

I found two ways to do it. One is by using import java.io.*; and the other is,
import java.util.Scanner;

If I assign a class name and start the console input from public static void main
both...
How to send console progress information to gui
Hi,

I am using java to send a commdline arg 'mklabel –config hello.o REL3'.
The output that I get is a new line for each element a put the label on.
In my application I want to show the user the progress of the command.
Can I create an event that contains the information for each line and...
I write a console java edition of top
I write a console java edition of top

http://code.google.com/p/hatter-source-code/wiki/jtop


How to create a batch file for Java Console Application?
I have a Java console application.

It needs to be decompressed and then found
on the hard drive. One has to keep double clicking until
one finds the folder that says Resources. In this
folder there will be a program that says Contents.
In that folder, ones clicks on the...
How can I read from the entire standard input at once?
The following code is a stripped down version of a large project. The
question is why do I have to press ctrl z every time to get the
output? That is, say I type the following at the command line

1 - 3

and then press the enter key on my keyboard. I press ctrl z and I get
'The value is 1'...
Read java Annotation field values from class
How to read java Annotation field values from reflect class.
I am having a java class file Myclass.java. This file having the
annotation

@interface MyAnnotation {
boolean DuplicateValues();
boolean DuplicateVariables();
}
@MyAnnotation(DuplicateValues = true, DuplicateVariables =...