AddressData
读取速度贼快的地址库,包含省市区及身份证号前缀
-
地址库大小 54.14746KB
-
读取耗时 14~25ms (MacBook Pro LQ2 i7-4770HQ)
{北京市={市辖区={东城区=110101, 西城区=110102, 崇文区=110103,...
代码
package com.address;import java.io.BufferedInputStream;import java.io.EOFException;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.LinkedHashMap;/*** Created by wanjian on 2017/11/15.*/public class Read { public static void main(String[] args) throws IOException { String file = "area.bin"; System.out.println("地址库大小/KB " + new File(file).length() / 1024f); long s = System.currentTimeMillis(); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); LinkedHashMap>> map = read(inputStream); inputStream.close(); System.out.println("读取耗时/ms " + (System.currentTimeMillis() - s)); System.out.println(map); } private static LinkedHashMap >> read(InputStream in) throws IOException { BufferedInputStream inputStream = new BufferedInputStream(in); LinkedHashMap >> provinceSet = new LinkedHashMap<>(); byte provinceSetSize = readByte(inputStream); for (int i = 0; i < provinceSetSize; i++) { String provinceName = readString(inputStream); LinkedHashMap > citySet = new LinkedHashMap<>(); provinceSet.put(provinceName, citySet); byte citySetSize = readByte(inputStream); for (int j = 0; j < citySetSize; j++) { String cityName = readString(inputStream); LinkedHashMap areaSet = new LinkedHashMap<>(); citySet.put(cityName, areaSet); byte areaSetSize = readByte(inputStream); for (int k = 0; k < areaSetSize; k++) { String areaName = readString(inputStream); int code = read3Byte(inputStream); areaSet.put(areaName, code); } } } return provinceSet; } private static String readString(InputStream inputStream) throws IOException { byte[] str = new byte[readByte(inputStream)]; int read = 0; int count; while ((read < str.length)) { count = inputStream.read(str, read, str.length - read); if (count < 0) { throw new EOFException("address file maybe incomplete"); } read += count; } return new String(str, "UTF-8"); } private static int read3Byte(InputStream inputStream) throws IOException { int h = inputStream.read(); if (h < 0) { throw new EOFException("address file maybe incomplete"); } int m = inputStream.read(); if (m < 0) { throw new EOFException("address file maybe incomplete"); } int l = inputStream.read(); if (l < 0) { throw new EOFException("address file maybe incomplete"); } return h << 16 | m << 8 | l; } private static byte readByte(InputStream inputStream) throws IOException { int v = inputStream.read(); if (v < 0) { throw new IOException("address file maybe incomplete"); } return (byte) v; } /* 生成地址库文件 public static void geneAddressFile(LinkedHashMap >> provinceSet) throws IOException { OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("/area.bin")); writeByte((byte) provinceSet.size(), outputStream); for (Map.Entry >> province : provinceSet.entrySet()) { String provinceName = province.getKey(); writeString(provinceName, outputStream); LinkedHashMap > citySet = province.getValue(); writeByte((byte) citySet.size(), outputStream); for (Map.Entry > city : citySet.entrySet()) { String cityName = city.getKey(); writeString(cityName, outputStream); LinkedHashMap areaSet = city.getValue(); writeByte((byte) areaSet.size(), outputStream); for (Map.Entry codeSet : areaSet.entrySet()) { String codeName = codeSet.getKey(); writeString(codeName, outputStream); int code = codeSet.getValue(); write3Byte(code, outputStream); } } } outputStream.close(); } private static void writeString(String s, OutputStream outputStream) throws IOException { byte bytes[] = s.getBytes("UTF-8"); writeByte((byte) bytes.length, outputStream); outputStream.write(bytes); } private static void writeByte(byte b, OutputStream outputStream) throws IOException { outputStream.write(b); } private static void write3Byte(int v, OutputStream outputStream) throws IOException { outputStream.write((v >> 16) & 0xFF); outputStream.write((v >> 8) & 0xFF); outputStream.write(v & 0xFF); } */}复制代码