将exception写入文件
我创建的java项目将测试1800个案例,每个案例的输出必须与黄金(所需)输出相匹配。 我为此创建了一个perl脚本并在cygwin上运行它。
有一些案例会抛出exception但却被错误地认为是正确的。 我想在java代码中添加一个try catch块,这样如果抛出任何exception,就会捕获它,并在文件exception.txt
上打印堆栈跟踪。
Pseudo Java code: main() { try { ... //complete code of main() } catch (Exception e) { FileWriter fstream=new FileWriter("exception.txt"); BufferedWriter out=new BufferedWriter(fstream); out.write(e.toString()); out.close(); } }
但是这会覆盖以前的文件内容,最后文件包含最后抛出的exception。 如何编写catch块以便打印stackTrace并且文件内容完好无损且每次都不会被覆盖。
请改用此构造函数:
new FileWriter ("exception.txt", true);
这里描述。
编辑 :根据Jon的评论如下:
如果要打印整个堆栈跟踪,请使用printStackTrace
:
fw = new FileWriter ("exception.txt", true); pw = new PrintWriter (fw); e.printStackTrace (pw);
此外,在此之后使用适当的close
呼叫。
您可以使用:
FileOutputStream fos = new FileOutputStream(new File("exception.txt"), true); PrintStream ps = new PrintStream(fos); e.printstacktrace(ps);
使用
FileWriter fstream=new FileWriter("exception.txt", true);
创建一个附加文件编写器。
这是一个程序,演示了我认为你需要的东西:
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.PrintWriter; public class StrackTraceAppender { public static void main(String[] args) { try { thrower("Oh noes!"); } catch (Exception e) { appendToFile(e); } try { thrower("I died!"); } catch (Exception e) { appendToFile(e); } } public static void thrower(String message) throws Exception { throw new RuntimeException(message); } public static void appendToFile(Exception e) { try { FileWriter fstream = new FileWriter("exception.txt", true); BufferedWriter out = new BufferedWriter(fstream); PrintWriter pWriter = new PrintWriter(out, true); e.printStackTrace(pWriter); } catch (Exception ie) { throw new RuntimeException("Could not write Exception to file", ie); } } }
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.PrintWriter; public class StrackTraceAppender { public static void main(String[] args) { try { thrower("Oh noes!"); } catch (Exception e) { appendToFile(e); } try { thrower("I died!"); } catch (Exception e) { appendToFile(e); } } public static void thrower(String message) throws Exception { throw new RuntimeException(message); } public static void appendToFile(Exception e) { try { FileWriter fstream = new FileWriter("exception.txt", true); BufferedWriter out = new BufferedWriter(fstream); PrintWriter pWriter = new PrintWriter(out, true); e.printStackTrace(pWriter); } catch (Exception ie) { throw new RuntimeException("Could not write Exception to file", ie); } } }
它使用Throwable上的printStackTrace(PrintWriter)
方法将整个堆栈跟踪打印到名为“exception.txt”的文件的末尾,然后有一个main()
方法,它演示了两个示例exception的用法。 如果你在IDE中运行它,你会发现你得到一个写有两个堆栈跟踪的文件(适合我)。
尝试这个:
PrintStream printStream = new PrintStream(new File("exception.txt")); try { //error proven code } catch (Exception exception) { exception.printStackTrace(printStream); }
尝试这个:
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.*; public class ErrorLogger { private Logger logger; public ErrorLogger() { logger = Logger.getAnonymousLogger(); configure(); } private void configure() { try { String logsFolder = "logs"; Files.createDirectories(Paths.get(logsFolder)); FileHandler fileHandler = new FileHandler(logsFolder + File.separator + getCurrentTimeString() + ".log"); logger.addHandler(fileHandler); SimpleFormatter formatter = new SimpleFormatter(); fileHandler.setFormatter(formatter); } catch (IOException exception) { exception.printStackTrace(); } addCloseHandlersShutdownHook(); } private void addCloseHandlersShutdownHook() { Runtime.getRuntime().addShutdownHook(new Thread(() -> { // Close all handlers to get rid of empty .LCK files for (Handler handler : logger.getHandlers()) { handler.close(); } })); } private String getCurrentTimeString() { DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); return dateFormat.format(new Date()); } public void log(Exception exception) { logger.log(Level.SEVERE, "", exception); } }
用法:
ErrorLogger errorLogger = new ErrorLogger(); try { throw new Exception("I died!"); } catch (Exception exception) { errorLogger.log(exception); }