代码改变世界

根据身份证号获取地址

2015-11-16 17:02  安妮云朵  阅读(1029)  评论(0)    收藏  举报
private static String getCurrName(String provinceId) {
		try {
			LineNumberReader lnr = new LineNumberReader(
					new InputStreamReader(DataPrepareUtil.class
							.getResourceAsStream("/idcard_address.txt"), "GBK"));
			String line = lnr.readLine();
			while (line != null) {
				String[] str = line.replace("  ", "").split(" ");
				int code = Integer.parseInt(StringUtils.trim(str[0]));
				if (provinceId.length() == 2
						&& Integer.parseInt(provinceId) * 10000 == code) {
					return StringUtils.trim(str[1]);

				}
				if (provinceId.length() == 4
						&& Integer.parseInt(provinceId) * 100 == code) {
					return StringUtils.trim(str[1]);
				}
				if (provinceId.length() == 6
						&& Integer.parseInt(provinceId) == code) {
					return StringUtils.trim(str[1]);
				}

				line = lnr.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return null;
	}