File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Getting all data in the consol and dumping it into a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Getting all data in the consol and dumping it into a text file" Watch "Getting all data in the consol and dumping it into a text file" New topic
Author

Getting all data in the consol and dumping it into a text file

Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Heya,

I'm trying to copy all the data from written to the console (incl errors, system.outs.. est) and write it to a text file. I know how to write it, but i have no clue how to get the data out of the actual console(Not the log file). Any ideas how this can be done?

Thanks


Courage is not the absence of fear but rather the judgment that something is more important then fear. ~ Ambrose Redmond
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Check the following link.

Hope it helps.


SCJP, SCWCD
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Thanks, but I'm a bit confused, if i'm correct that's basically creating a log file? We have a log file being created and updated constantly(deals with server side), but we also have a console(deals with client side) Some errors that appear in the console don't necessarily appear in the log file and visa versa. I only need to write the console if an exception occurs, i don't need to write it or constantly update it if that makes any sense.

Thanks
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hm ... in that case I guess you only need to write err stream, right?
Try the following example: redirect.

Does this help?
If not, what about this one?

Perhaps I just don't understand what you're trying to do. ;)
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
example: redirect --> seems to be almost exactly what i need, although i need to write the complete console if an exception occurs kind of like copy and paste. The other thing i'm not really getting is where would i implement that code? (sorry for the stupid question ) But the console is a default function in java, what part is actaully accessing the original console data? Would then contain the original data?

Thanks
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Regarding the second link ... I guess it's the answer to:
Lila Fowler wrote:Some errors that appear in the console don't necessarily appear in the log

When redirecting the console output to a file, you have to use "2>&1".
Otherwise, you'll only see standard output ...

Lili Fowler wrote:But the console is a default function in java, what part is actually accessing the original console data?Would

1. orgStream = System.out;

then contain the original data?

Well, printing to console output is indeed the default operation of stdin, stdout & stderr.
However with System.setXXX(...), you have the possibility to redirect it to e.g. a file.
So in above case it only contains the ref to the System.out, which is later used to restore the printing back to the console.


Regards,
Rok
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
No longer a "beginning" question. Moving thread.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Hi, sorry it took me so long to reply but I was moved to to other tickets.

Here is my code, but it still doesn't write the exception to the file, and the things that do write to the file don't display in the console. I need the .txt file to be a mirror image of the console. Any ideas?

Thanks.

Here is my current code.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

That little hello is printed before the exception. That's because the control flow is like this:
- get a reference to the old streams (lines 3-4)
- set up the new streams (lines 5-7)
- test the redirect (lines 8-9)
- throw a RuntimeException (line 10)
- run the finally block to restore the old streams (lines 29-30)
- exit the main method with the RuntimeException (JVM internals)
- print the RuntimeException to the default, restored standard error stream (JVM internals)

If you want to write everything to your custom print stream simply don't call lines 29 and 30. If the JVM exits it will restore the default output and error streams and close all file handles opened from that Java application for you.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Thanks, that writes everything to my file as you said it would(took out lines 29 & 30), but i still need it in my console. Is there a way for the data to be printed in my both in the console as well as the .txt?
Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You could create or find something called a "tee" output stream (named after the Unix tool "tee"). This basically duplicates data written to it. Apache Commons IO has such a class.

How it would work:
From now on everything written to either System.out or System.err will be written to both the original System.out and the file.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Thanks its now writing to both the file & the console, but the text has lost its color & functionality. eg if you had an exception it would be printed in red, similarly with System.err I understand its because you write it to the file and then to the console, but is it maybe possible to let it write to the console first & then to the file? Or a way to check the color and replace it when it is rewritten? I got it to display just red but not both black & red as it would originally write to the console.

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

The console does not use colors. It is your IDE (Eclipse, Netbeans, ...) that adds these colors for System.err.
Lila Fowler
Ranch Hand

Joined: Jul 31, 2009
Posts: 84
Ok, in that case i think i'm done. Thanks a million
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting all data in the consol and dumping it into a text file
 
Similar Threads
read text file and save data into database
how to read a binary file
Console Output of a command to file
writing into a file
Trouble with getting input from keyboard