d0e1f 发表于 2015-07-10 13:07

Java处理中华人民共和国行政区划代码

代码从中华人民共和国国家统计局http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201504/t20150415_712722.html 获取最新县及县以上行政区划代码(截止2014年10月31日),copy 下来,转换成以下格式
110000北京市
110100市辖区
110101东城区
110102西城区
110105朝阳区
110106丰台区
110107石景山区
110108海淀区
110109门头沟区
110111房山区
110112通州区
110113顺义区
110114昌平区
110115大兴区
110116怀柔区
110117平谷区
110200县
110228密云县
110229延庆县
120000天津市
120100市辖区
120101和平区
120102河东区
120103河西区
120104南开区
......
的文件

定义Area类进行接收
public class Area {
private String code ;
//行政编码
private String name;
//名称
private int level;
//行政级别 0:省/直辖市 1:地级市 2:县级市
private String parentCode;
//上一级的行政区划代码

public Area() {
super();
}

public Area(String code, String name, int level, String parentCode) {
super();
this.code = code;
this.name = name;
this.level = level;
this.parentCode = parentCode;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getLevel() {
return level;
}

public void setLevel(int level) {
this.level = level;
}

public String getParentCode() {
return parentCode;
}

public void setParentCode(String parentCode) {
this.parentCode = parentCode;
}

public String outPutString(){
return this.getCode()+" "+this.getName()+" "+this.getLevel()+" " +this.getParentCode();
}
}

通过文件流进行数据读写操作,转换成对象

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AreaDemo {

public List<Area> BufferedReaderDemo(String path){
String line = null;
BufferedReader reader = null;
File file = new File(path);

String cityCode="";
String countyCode="";
List<Area> result = new ArrayList<Area>();
if(!file.exists()||file.isDirectory()){
   return null;
}
try {
   FileReader in = new FileReader(file);
   reader = new BufferedReader(in);
   
//读取文件的每一行
   while((line = reader.readLine())!=null){
    String[] data = this.doString(line);
   
//处理读取的文件记录
    if(this.isSheng(data)){
   cityCode = data;
   Area area = new Area(data, data, 0, "0");
   result.add(area);
    }else if(this.isShi(data)){
   countyCode =data;
   Area area = new Area(data, data, 1, cityCode);
   result.add(area);
    }else{
   Area area = new Area(data, data, 2, countyCode);
   result.add(area);
    }
   
   }
   
} catch (FileNotFoundException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}finally{
   try {
    reader.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
}
   
return result;
   
}

//字符分割
public String[] doString(String line){
String code="";
String name="";
code = line.substring(0, 6);
name = line.substring(6, line.length());
String[] result = new String []{code,name};
return result;
}

//判断是否省或者直辖市
public boolean isSheng(String code){
String last = code.substring(2);
if("0000".equalsIgnoreCase(last)){
   return true;
}
return false;
   
}
//判断是否地级市
public boolean isShi(String code){
String last = code.substring(4);
if("00".equalsIgnoreCase(last)){
   return true;
}
return false;
}


}

拿到list列表之后,不管是保存文件还是写入数据库,都比较方便了。
页: [1]
查看完整版本: Java处理中华人民共和国行政区划代码