读取速度贼快的省市区地址库
生活随笔
收集整理的這篇文章主要介紹了
读取速度贼快的省市区地址库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
AddressData
讀取速度賊快的地址庫,包含省市區(qū)及身份證號前綴
-
地址庫大小 54.14746KB
-
讀取耗時(shí) 14~25ms (MacBook Pro LQ2 i7-4770HQ)
{北京市={市轄區(qū)={東城區(qū)=110101, 西城區(qū)=110102, 崇文區(qū)=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<String, LinkedHashMap<String, LinkedHashMap<String, Integer>>> map = read(inputStream);inputStream.close();System.out.println("讀取耗時(shí)/ms " + (System.currentTimeMillis() - s));System.out.println(map);}private static LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, Integer>>> read(InputStream in) throws IOException {BufferedInputStream inputStream = new BufferedInputStream(in);LinkedHashMap<String, LinkedHashMap<String, LinkedHashMap<String, Integer>>> provinceSet = new LinkedHashMap<>();byte provinceSetSize = readByte(inputStream);for (int i = 0; i < provinceSetSize; i++) {String provinceName = readString(inputStream);LinkedHashMap<String, LinkedHashMap<String, Integer>> citySet = new LinkedHashMap<>();provinceSet.put(provinceName, citySet);byte citySetSize = readByte(inputStream);for (int j = 0; j < citySetSize; j++) {String cityName = readString(inputStream);LinkedHashMap<String, Integer> 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<String, LinkedHashMap<String, LinkedHashMap<String, Integer>>> provinceSet) throws IOException {OutputStream outputStream = new BufferedOutputStream(new FileOutputStream("/area.bin"));writeByte((byte) provinceSet.size(), outputStream);for (Map.Entry<String, LinkedHashMap<String, LinkedHashMap<String, Integer>>> province : provinceSet.entrySet()) {String provinceName = province.getKey();writeString(provinceName, outputStream);LinkedHashMap<String, LinkedHashMap<String, Integer>> citySet = province.getValue();writeByte((byte) citySet.size(), outputStream);for (Map.Entry<String, LinkedHashMap<String, Integer>> city : citySet.entrySet()) {String cityName = city.getKey();writeString(cityName, outputStream);LinkedHashMap<String, Integer> areaSet = city.getValue();writeByte((byte) areaSet.size(), outputStream);for (Map.Entry<String, Integer> 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);}*/ }復(fù)制代碼源碼地址 https://github.com/android-notes/AddressData
總結(jié)
以上是生活随笔為你收集整理的读取速度贼快的省市区地址库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Golang 的跨平台交叉编译浅析
- 下一篇: 解决问题:心态 原则 方法