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.
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
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.
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.