how to write exception information into a txt file?

i got a program which generate lots of exception information. its hard to see from the screen, is there any way to make the input into some txt file?

when i use

try

{

...

}catch(Exception ex)

{

// to do something here and make the output into a txt file?

}

[314 byte] By [JbloodHa] at [2007-10-2 17:16:21]
# 1
ex.printStackTrace(System.err);
BIJ001a at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

depending upon the version of jdk being used one of the options will be to fetch the StackTraceElement array and writing each of the element using the standard file io operation

StackTraceElement[] elements = ex.getStackTrace();

int noOfElements = elements.length;

for (int i=0; i<noOfElements;i++)

{

//fetch the stacktrace element and write to a file.

}

Thanks

Rahul>

Rahul_2001a at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3
thanks for ur reply, i tried catch(Exception ex){ ex.printStackTrace(System.err);}but seemsit doesnt write the exception infor into any txt file?
JbloodHa at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

my sdk version is jdk1.5.0_06

when u said "//fetch the stacktrace element and write to a file"

i tried this

StackTraceElement[] elements = ex.getStackTrace();

int noOfElements = elements.length;

for (int i=0; i<noOfElements;i++)

{

fetchErrToLog(elements); // this method is to write string into a file

}

and then it ask me to include another try{}catch(){} to cover the fetchErrToLog() method.... and seems it not working too>

JbloodHa at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
>but seemsit doesnt write the exception infor into any txt file?javac *.java >stdout.txt 2>stderr.txt
BIJ001a at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

You can redirect the standard output and standard error streams into files. See the example above for how to do it from the starting environment (prompt/shell).

Besides, you can programatically reassign stdout and stderr.

// java.lang.System

public static void setOut(PrintStream out)

//Reassigns the "standard" output stream.

public static void setErr(PrintStream err)

//Reassigns the "standard" error output stream.

BIJ001a at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7
COOOOOOOOOOOOOOL!THAT'S EXACTLY WHAT I NEED, actually when i did like this: java -cp . program >log.txt 2>err.txt
JbloodHa at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 8
another way is similar to mentioned above....try{logWriter=new printWriter(new File(path));}catch{}.......catch(Exception ex){ex.printStackTrace(logWriter);logWriter.flush();}
s-e-r-g-ea at 2007-7-13 18:31:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...