wood burning stoves 2.0*
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
Author

Crecate seperate file for Exception

dhan dd
Greenhorn

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");
f.mkdirs();
BufferedWriternew obj=BufferedWriter(obj);
obj.write("dhan");

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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
Rob,
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
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Originally posted by Gamini Sirisena:
Rob,
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: 10523
    
    9

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
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
. . . and "dhan dd" please check the administrative private message I sent you a minute ago.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Crecate seperate file for Exception