File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Crecate seperate file for Exception

 
dhan dd
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob,
Do you mean a separate process?
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and "dhan dd" please check the administrative private message I sent you a minute ago.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic