工做中後臺拋出的異常ID集中在一個文件中,報錯的時候須要在前臺把具體的異常信息作爲對話框的內容彈出來。由於後臺拋異常的時候只會把異常ID傳到前臺,因此後臺須要在國際化文件註冊一下異常ID和對應的異常信息而後出錯的時候前臺根據異常ID去國際化文件註冊,常常有同事在後臺拋異常可是忘記註冊,寫了一點代碼用來檢查異常ID是否在國際化文件都註冊了和國際化文件是否註冊了冗餘的異常ID
java
package com.victor.check.exception; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; public class CheckExceptionID { public static void main(String[] args) throws Exception { checkArgs(args); checkExceptionID(args); } private static void checkArgs(String[] args) throws Exception { if(args.length != 2) { throw new Exception("The param has something wrong, please have a check!"); } } private static void checkExceptionID(String[] args) throws Exception { File exceptionFile = new File(args[0]); File i18nFile = new File(args[1]); if(!exceptionFile.exists()) { throw new FileNotFoundException("The file: " + args[0] + " you want to parse not exists. "); } List<String> exceptionIDList = getExceptionIDList(exceptionFile); List<String> i18nExceptionIDList = getExceptionIDList(i18nFile); for(String exceptionID : exceptionIDList) { checkIsNeedToRegiser(i18nExceptionIDList, exceptionID + "_desc"); checkIsNeedToRegiser(i18nExceptionIDList, exceptionID + "_detail"); checkIsNeedToRegiser(i18nExceptionIDList, exceptionID + "_reason"); checkIsNeedToRegiser(i18nExceptionIDList, exceptionID + "_advice"); } for(String i18nException : i18nExceptionIDList) { if(!i18nException.contains("_")) { System.err.println(i18nException + " not need to register in the i18n file"); } else { if(!exceptionIDList.contains(i18nException.substring(0, i18nException.indexOf("_")))) { System.err.println(i18nException + " not need to register in the i18n file"); } } } } private static List<String> getExceptionIDList(File file) throws Exception { List<String> exceptionIDList = new ArrayList<String>(); FileInputStream fin = new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fin)); String line; while ((line = br.readLine()) != null) { if(line.indexOf("=") == -1) { continue; } else { exceptionIDList.add(line.split("=")[0]); } } return exceptionIDList; } private static void checkIsNeedToRegiser(List<String> i18nExceptionIDList, String id) { if(!i18nExceptionIDList.contains(id)) { System.err.println(id + " need to registered in the i18n file."); } } }
Export成一個JAR文件spa
用cmd切換到jar所在的路徑下code
輸入java -jar fileName.jar args[0] args[1]運行,其中args[0]是後臺拋出異常ID的文件,args[1]是註冊的國際化文件
get