该工具类的用途:为了读取国家最新区域信息复制到txt中,然后再使用该程序生成SQL脚本插入到数据库中的区域表里。
这是在本人需要实现省市区县三级联动的时候写的一个非常简单且低级的工具类,希望对跟我一样在实习的同学有所帮助吧,同时也是希望记录一下自己的工作成果。刚进公司没多久的时候,经理就让我写一个脚本,用来实现上述功能。说实话 ,当时一直在纠结脚本是什么。后来开始动手 ,才发现只是写一个工具类来往数据库里插入新的数据。总之进入公司以后就感觉要坚持学习,因为谁都不知道下个任务到底是不是你擅长的,领导不会因为你不会就把任务给别人。区域划分信息及编码链接:http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html
1 public class AreaUtil { 2 3 private static String SQL_TPL = "insert into ggx_area_code(region_code,region_name,parent_code,region_level,region_name_en,region_shotname_en)values(\"%s\",\"%s\",%s,%s,\"%s\",\"%s\");"; 4 5 public static void main(String[] args) throws Exception { 6 File areaFile = new File("d://area.txt"); 7 File sqlFile = new File("d://area.sql"); 8 BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(areaFile), "gbk")); 9 String line = null; 10 String proCode = null; 11 String cityCode = null; 12 StringBuffer sb = new StringBuffer(); 13 while ((line = reader.readLine()) != null) { 14 int level = getLevel(line); 15 String[] codes = parse(line); 16 String code = codes[0]; 17 String name = codes[1]; 18 String sql = ""; 19 if(level == 1){ 20 //省 21 proCode = code; 22 System.out.println(name); 23 sql = createProvineSql(code,name); 24 }else if(level == 2){ 25 //市 26 cityCode = code; 27 sql = createCitySql(proCode, code, name); 28 }else{ 29 //区县 30 sql = createCountrySql(cityCode, code, name); 31 } 32 sb.append(sql).append("\n"); 33 } 34 reader.close(); 35 FileUtils.writeStringToFile(sqlFile, sb.toString(), "utf-8"); 36 37 } 38 39 private static String createProvineSql(String code,String name){ 40 return String.format(SQL_TPL, code,name,"null",1,PinyinUtil.getPinYin(name),PinyinUtil.getPinYinHeadChar(name)); 41 } 42 43 private static String createCitySql(String pcode,String cityCode,String name){ 44 return String.format(SQL_TPL, cityCode,name,"\"" + pcode + "\"",2,PinyinUtil.getPinYin(name),PinyinUtil.getPinYinHeadChar(name)); 45 } 46 47 private static String createCountrySql(String cityCode, String countryCode, String name){ 48 return String.format(SQL_TPL, countryCode,name,"\"" + cityCode + "\"",3,PinyinUtil.getPinYin(name),PinyinUtil.getPinYinHeadChar(name)); 49 } 50 51 // 只能判断部分CJK字符(CJK统一汉字) 52 public static boolean isChineseByREG(String str) { 53 if (str == null) { 54 return false; 55 } 56 Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+"); 57 return pattern.matcher(str.trim()).find(); 58 } 59 60 private static String[] parse(String line){ 61 String[] rlt = {"",""}; 62 for (int i = 0; i < line.length(); i++) { 63 String item = line.substring(i, i+1).trim(); 64 if(!"".equals(item)){ 65 if(item.matches("\\d+")){ 66 rlt[0] += item; 67 }else if(isChineseByREG(item)){ 68 rlt[1] += item; 69 } 70 } 71 } 72 return rlt; 73 } 74 75 /** 76 * 获取省市县层级 77 * 78 * @param line 79 * @return 80 */ 81 private static int getLevel(String line){ 82 if(line.charAt(0) != 12288){ 83 return 1; 84 }else{ 85 if(line.charAt(1) == 12288){ 86 return 3; 87 } 88 return 2; 89 } 90 } 91 }