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

本文共 4647 字,大约阅读时间需要 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); } */}复制代码

转载地址:http://allwo.baihongyu.com/

你可能感兴趣的文章
hdu2276 快速矩阵幂
查看>>
vim制表符占位个数修改
查看>>
JSP内置对象值out对象及其它的一些常见方法
查看>>
Android undefined intent constructor错误?
查看>>
typscript 语法1
查看>>
04 对象与类
查看>>
headfirst python 03, 04
查看>>
Git在Githib和Github上的使用
查看>>
mysql使用学习的帮助文档
查看>>
Apache Rewrite规则详解
查看>>
SQL细小知识点
查看>>
linux系统调用的三种方法
查看>>
bzoj 2818 欧拉函数
查看>>
【cisco探索之路】
查看>>
Python条件语句
查看>>
JavaScript小结
查看>>
python Web开发你要理解的WSGI & uwsgi详解
查看>>
基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境
查看>>
SQL语言:DDL/DML/DQL/DCL
查看>>
swift代理使用
查看>>