需求:項目裏面的日誌不可以打印出異常棧java
分析:項目裏面的異常分爲兩種異常,一種是業務異常好比登陸次數過多,好比密碼不對等等,約定這樣的異常不用處理。因此咱們只須要打印出特徵碼不太明顯的常規異常便可,廢話少說直接上代碼測試
public class FileInsertRow { public static void main(String args[]) { try { List<String> list = new ArrayList<String>(); File pathFiles = new File("D:\\log\\files.text"); BufferedReader reader = null; try { String tempString = null; reader = new BufferedReader(new FileReader(pathFiles)); while ((tempString = reader.readLine()) != null) { list.add(tempString); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } FileInsertRow test = new FileInsertRow(); for (String pathName : list) { System.out.println("處理完成的文件數"+pathName); List<Integer> listNumbers = readFileByLines(pathName); File srcFile = new File(pathName);// 首先存在文件,文件內容:1 if(!listNumbers.isEmpty()){ System.out.println("駕駛"+pathName); test.insertStringInFile(srcFile,listNumbers, " log.info(\"cause異常信息上:\",e);"); } } } catch (Exception e) { e.printStackTrace(); } } public void traverseFolder2(String path,List<String> list) { File file = new File(path); if (file.exists()) { File[] files = file.listFiles(); if (files.length == 0) { // System.out.println("文件夾是空的!"); return; } else { for (File file2 : files) { if (file2.isDirectory()) { // System.out.println("文件夾:" + file2.getAbsolutePath()); traverseFolder2(file2.getAbsolutePath(),list); } else { if(file2.getName().endsWith("Module.java")){ list.add(file2.getAbsolutePath()); } } } } } else { System.out.println("文件不存在!"); } } /** * 以行爲單位讀取文件,經常使用於讀面向行的格式化文件 * @throws Exception */ public static List<Integer> readFileByLines(String fileName) throws Exception { List<Integer> lineNumbers = new ArrayList<Integer>(); File file = new File(fileName); BufferedReader reader = null; try { System.out.println("以行爲單位讀取文件內容,一次讀一整行:"); reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次讀入一行,直到讀入null爲文件結束 while ((tempString = reader.readLine()) != null) { // 顯示行號 if(tempString.contains("catch")&&tempString.contains("Exception")){ //獲取括弧裏面的字符串 int left =tempString.indexOf("("); int right = tempString.indexOf(")"); String exinfo = tempString.substring(left+1, right); // System.out.println("***"+exinfo+"****"); String[] execptionstrings = exinfo.split(" "); if(execptionstrings[0].equals("Exception")){ lineNumbers.add(line); } } line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return lineNumbers; } public void insertStringInFile(File inFile, List<Integer> linenos, String lineToBeInserted) throws Exception { // 臨時文件 File outFile = File.createTempFile("name", ".tmp"); // 輸入 FileInputStream fis = new FileInputStream(inFile); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); // 輸出 FileOutputStream fos = new FileOutputStream(outFile); PrintWriter out = new PrintWriter(fos); // 保存一行數據 String thisLine; // 行號從1開始 int i = 1; while ((thisLine = in.readLine()) != null) { // 若是行號等於目標行,則輸出要插入的數據 for (int lineno : linenos) { if (i == (lineno+1) ){ System.out.println("插入日誌"+lineToBeInserted); out.println(lineToBeInserted); } } // 輸出讀取到的數據 out.println(thisLine); // 行號增長 i++; } out.flush(); out.close(); in.close(); // 刪除原始文件 inFile.delete(); // 把臨時文件更名爲原文件名 outFile.renameTo(inFile); TimeUnit.SECONDS.sleep(10); } }
分爲這麼幾步this
1.獲取項目當中全部的java文件日誌
2.遍歷該文件查看是否包含hot wordcode
3.在下面一行打印出日誌便可。字符串
精測試,以上代碼可行性極高,在寫這段代碼的時候,也入了很多坑,好比java文件操做的幾個類,讀寫文件的亂碼問題等等。有空再寫,可是依着我拖延症的毛病,估計是寫不上了````get