Question

I want my application to create an error.log if, and only if, an error occurs. So I did this:

System.setErr(new PrintStream("error.log"));

Works as expected in Eclipse - error happens, error.log with error message pops up in the source dir.. The exported application, however, creates a completely empty log file right on program start, which is annoying. What's the reason for this behavior?

Was it helpful?

Solution

From the PrintStream docs:

fileName - The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

This code creates the file in my version of NetBeans. You could create the PrintStream on the first error, instead of creating it no matter what.

OTHER TIPS

Have a look at the PrintStream api

When you call new PrintStream if the error.log file does not exist, it will create it.

http://docs.oracle.com/javase/7/docs/api/java/io/PrintStream.html#PrintStream(java.lang.String)

fileName - The name of the file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top