Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes Crecate seperate file for Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Crecate seperate file for Exception" Watch "Crecate seperate file for Exception" New topic

Crecate seperate file for Exception

dhan dd

Joined: Aug 01, 2008
Posts: 3
I am developing one project and in that project i want to generate a seperate file for Exception and console output. That file will generate automatically. Ex. 'Exception' this is name of folder. And it contains notepad file in which exception, errors and messages should print.
I am using following code to create folder and notepad file for exception.
This following code is just for idea.

File f=new File(System.getProperty ("Dhan.dir") + System.getProperty ("file.separator")+"Exception"+System.getProperty ("file.separator")+"Exception2");
BufferedWriternew obj=BufferedWriter(obj);

If any one have an idea, please sugest for this.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Throwable.printStackTrace is overloaded to accept a PrintStream or PrintWriter as parameter. You could use those to write the exception itself:

You can also use PrintWriter to print or println other values.

You can also choose to keep the PrintWriter open during the execution of your program, but that does mean the file can be locked by the operating system and be unavailable for other programs. Windows is quite notorious for that.

And never ever forget to close your Readers, Writers and Streams

How To Ask Questions How To Answer Questions
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 378
Do you mean a separate process?
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Easiest way to obtain this is to use the Logger-api (or Log4J) and have all exceptions and errors logged. Then you have the choice to write them to console, file, database, rss or whatever you want, by configure the loggers to write to an appender.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20276

Originally posted by Gamini Sirisena:
Do you mean a separate process?

That want to read or write to your file while the Java application has locked it? Yes.
But also quite possibly other threads within the Java application itself can find out the file is not available.

It's basically a decision that needs to be taken, and explained: what is more important, speed (thereby not opening and closing the file all the time), or being gentle towards other threads and processes.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10828

Check out

This way all your e.printStackTrace() statements will be redirected to the "err" you define and the System.out.print() statements will be redirected to the "out" you define.

In this case, if I am not mistaken, you dont need to bother closing the streams explicitly.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46410
. . . and "dhan dd" please check the administrative private message I sent you a minute ago.
I agree. Here's the link:
subject: Crecate seperate file for Exception
It's not a secret anymore!