catch捕捉到的异常写入文件中,如下是一个测试类 @Test public void test3() throws JSONException, IOException { List list = new ArrayList(); String url1= System.currentTimeMillis()+".txt"; list.add(1); try{ list.get(5); }catch (Exception ee){ ee.printStackTrace(); File file = new File("E:\\"+url1); FileWriter fw = null; if(!file.exists()) { file.createNewFile(); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ee.printStackTrace(new PrintStream(baos)); String writeDate = "当前时间:" + new Date() + "-------" + "error" + baos.toString(); try { fw = new FileWriter(file, true); fw.write(writeDate + "\r\n"); } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { fw.close(); } } } }