工做中后台抛出的异常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