Capturing all the text that I output to the console
HOw do I capture all the text I output to the console to a file. I know how to open filebuffers and all that, well I don't really know, but I have some sample code that I can follow. I am just wondering if I am suppose to just open the file buffer, then run my normal application with all the system.out.println() statements and then have that fed to a file? Right now I am using a lot of println() statements to debug, and I would like to be able to push it into a file so I can look at it later, without having that console line limitation.
My guess is to just open up a filestream and have it open and then run my normal code and then close the filestream at the end. Can somebody let me know if that is good enough...
let say I have a static method hat outputs a bunch of text, can I do this
filestream open
static method run
filestream close
and that's it.
1. Open a PrintStream and point it to whatever file you want to record your text in.
2. Everytime you want to record your text (where you have your System.out statements now), instead use the print stream's handle to write to it, like "myOutFile.println(...)".
3. You should probably buffer the output stream with a BufferedOutputStream.
4. Look at any of the numerous I/O examples on this site.
Hey thanks, that makes a lot of sense now. Why buffer though? What is the advantage of buffering. I've used a buffer readstream before just so that you could kind of peek ahead and see what the arguments for a function call was, but should I really use it even if I know what I am
Well, The method suggested is fine - but replaces the System.ou.println all over the place. If you have to again print the output to screen you might have problem.
A more elegant solution to the problem is to use the System.setOut(PrintStream ps) and set it to a file stream.
It is much easier & cleaner.
Ironluca@yahoo.com
can you please elaborate on your answer?I'm doing the following (which is wrong):logFile = new PrintWriter(new BufferedWriter( new FileWriter(logName)));System.setOut(new PrintStream(logFile));Thanks in advance
I found the answer, so here it is for those who might need it:ps = new PrintStream(new BufferedOutputStream( new FileOutputStream(logName)));System.setOut(ps);
> I found the answer, so here it is for those who might
> need it:
>
> ps = new PrintStream(new BufferedOutputStream( new
> FileOutputStream(logName)));
> System.setOut(ps);
erm ... <cough>
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(reader);
String input = buffer.readLine()
Hi,
Don't know if this will serve what you need,
but there is a simple DOS and UNIX way.
Let's say I have a test program called kstring.java.
1. javac kstring.java< compile.
2. java kstring<-- run the program.
Running the program resulted in several output
lines which comes out on the console. I can
redirect these output into a file, by:
3. java kstring > kstring.out
Now all the output goes into kstring.out
which I can later Edit access or print out.
This may help, if you are not familiar with
DOS or UNIX.
-- gte99te
> Hi,
>
> Don't know if this will serve what you need,
> but there is a simple DOS and UNIX way.
>
> Let's say I have a test program called kstring.java.
>1. javac kstring.java< compile.
>2. java kstring<-- run the program.
>
> Running the program resulted in several output
> lines which comes out on the console. I can
> redirect these output into a file, by:
>
>3. java kstring > kstring.out
>
> Now all the output goes into kstring.out
> which I can later Edit access or print out.
>
> This may help, if you are not familiar with
> DOS or UNIX.
>
> -- gte99te
>
>
>
Yeah that is how i was doing it, but i wanted to do it automatically without having to specify it at the command line. The option i posted before does that for you.
> > I found the answer, so here it is for those who
> might
> > need it:
> >
> > ps = new PrintStream(new BufferedOutputStream( new
> > FileOutputStream(logName)));
> > System.setOut(ps);
>
> erm ... <cough>
> InputStreamReader reader = new
> ew InputStreamReader(System.in);
> BufferedReader buffer = new
> ew BufferedReader(reader);
> String input = buffer.readLine()
uhmmm? The original post, and the answer i was interested in was about redirecting console output to a file, not reading console input.
I could see why you'd want to automatically
route output to a file, that normally would
go to the console. There might be use for
capturing output lines.
If the question is to avoid having to specify
the output on the command line, then what
one can do is to create a ".bat" file.
Ex.
kstring.bat
======================================
@java kstring > kstring.out
Now in executing, you'd just ==> kstring
Which will execute the kstring.bat,
and since "> kstring.out " is inside
the "bat", your output goes to the
kstring.out, instead of the console.
This would be another way, saves all the
redirection in Java code. Just use the
DOS command inside a xxx.bat file.
Just my opinion, may not work in your
circumstance.
-- gte99te