博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读取速度贼快的省市区地址库
阅读量:6910 次
发布时间:2019-06-27

本文共 4729 字,大约阅读时间需要 15 分钟。

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); } */}复制代码

转载于:https://juejin.im/post/5a217eb96fb9a0451b0459cc

你可能感兴趣的文章
Linux 积极使用swap空间
查看>>
【云计算的1024种玩法】第1招:制作一个浪漫的表白网页
查看>>
对象转换为数组
查看>>
label与input的结合方式
查看>>
单点登录实现原理(SSO)
查看>>
Mui框架支持微信支付宝支付源代码
查看>>
文件和目录权限
查看>>
su命令,sudo命令,限制root远程登录
查看>>
LINUX系统学习笔记Shell基础(一)认识shell、命令历史、命令补全、别名、通配符、管道符与前后台控制...
查看>>
有监督学习、无监督学习
查看>>
mybatis源码阅读(五) ---执行器Executor
查看>>
安装zibbix
查看>>
设计缓存系统该注意的问题
查看>>
svn服务器搭建
查看>>
[官方翻译]RabbitMQ生产上线前准备
查看>>
hanlp在Python环境中的安装失败后的解决方法
查看>>
分发系统介绍&expect脚本远程登录&expect脚本远程执行命令&expect脚本传递参数
查看>>
Haskell开发以太坊智能合约
查看>>
Java 8 函数式接口 - Functional Interface
查看>>
Python 正则表达式——re模块介绍
查看>>