java了解异常ID检测_检查异常ID是否在国际化文件中注册

工作中后台抛出的异常ID集中在一个文件中,报错的时候需要在前台把具体的异常信息做为对话框的内容弹出来。因为后台抛异常的时候只会把异常ID传到前台,所以后台需要在国际化文件注册一下异常ID和对应的异常信息然后出错的时候前台根据异常ID去国际化文件注册,经常有同事在后台抛异常但是忘记注册,写了一点代码用来检查异常ID是否在国际化文件都注册了和国际化文件是否注册了冗余的异常ID

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 exceptionIDList = getExceptionIDList(exceptionFile);

List 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 getExceptionIDList(File file) throws Exception

{

List exceptionIDList = new ArrayList();

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 i18nExceptionIDList, String id)

{

if(!i18nExceptionIDList.contains(id))

{

System.err.println(id + " need to registered in the i18n file.");

}

}

}

Export成一个JAR文件

用cmd切换到jar所在的路径下

输入java -jar fileName.jar args[0] args[1]运行,其中args[0]是后台抛出异常ID的文件,args[1]是注册的国际化文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值