How do I write the exception from printStackTrace() into a text file in Java?

后端 未结 5 1106
故里飘歌
故里飘歌 2020-12-03 03:00

I need to capture the exception in a text file in Java. For example:

try {
  File f = new File(\"\");
}
catch(FileNotFoundException f) {
  f.printStackTrac         


        
5条回答
  •  臣服心动
    2020-12-03 03:18

    Try to expand on this simple example:

    catch (Exception e) {
    
        PrintWriter pw = new PrintWriter(new File("file.txt"));
        e.printStackTrace(pw);
        pw.close();
    }
    

    As you can see, printStackTrace() has overloads.

提交回复
热议问题