博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 汉字转换为中文拼音的研究一:读取.db文件
阅读量:4031 次
发布时间:2019-05-24

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

最近好奇,中文汉字怎么转换为中文拼音,在开源中国找到源码后,便开始了研究。

源码参考地址:, 谢谢这位作者。

下载好源码后,调用它的API进行测试,可以实现我要的功能。但是,很好奇,其数据来源于何处。打开源文件后,发现有三个db文件.如图

把jar包后缀名修改为zip后,尝试打开该.db文件,试了很多方法都没有,有通过sqlite打开,通过excel打开,都未能成功。

最后自己写程序,读取该文件内容,然后写入txt文件,终于看到了改文件的庐山真面。代码如下

资源读取类

package org.lor.pinyin;import java.io.IOException;import java.util.Properties;import java.util.logging.Level;import java.util.logging.Logger;import java.util.zip.ZipInputStream;/** * 资源文件加载类 *  * @author Lor * @date 2014-8-12 * @version 1.0 * */public class PinyinResource {	// 日志对象	protected static final Logger logger = Logger.getLogger(PinyinResource.class			.getName());	/**	 * 获取资源属性	 * 	 * @param resourceName	 *            资源文件	 * @return 属性对象	 */	private static Properties getResource(String resourceName) {		ZipInputStream zip = new ZipInputStream(				PinyinResource.class.getResourceAsStream(resourceName));		try {			zip.getNextEntry();			Properties p = new Properties();			p.load(zip);			zip.close();			return p;		} catch (IOException e) {			logger.log(Level.WARNING, "IOException in loading PinyinResource", e);		}		return null;	}		/**	 * 获取拼音表属性	 * @return	 */	protected static Properties getPinyinTable() {		String resourceName = "/data/pinyin.db";		return getResource(resourceName);	}		/**	 * 获取多音字表属性	 * @return	 */	protected static Properties getMultiPinyinTable() {		String resourceName = "/data/mutil_pinyin.db";		return getResource(resourceName);	}	/**	 * 获取繁体简体字属性	 * @return	 */	protected static Properties getChineseTable() {		String resourceName = "/data/chinese.db";		return getResource(resourceName);	}	}
文件写入测试类

package test;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.util.Properties;import org.lor.pinyin.PinyinResource;public class ReadPropertiesTest extends PinyinResource {	public static void main(String[] args) throws IOException {		Properties p = getChineseTable();//getMultiPinyinTable();//getPinyinTable()		StringBuffer buffer = new StringBuffer(p.toString());		String charset = "UTF-8";		String file = "F:/chinese.txt";		FileOutputStream outputStream = new FileOutputStream(file);		OutputStreamWriter writer = new OutputStreamWriter(outputStream,charset);		try {			writer.write(buffer.toString());		} finally {			writer.close();		}	}}
最后可以看到三个文件的内容了。

截图如下

你可能感兴趣的文章
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
jQuery性能优化指南
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
在CentOS 7系统上搭建LNMP 环境
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
一个ahk小函数, 实现版本号的比较
查看>>
IP报文格式学习笔记
查看>>
autohotkey快捷键显示隐藏文件和文件扩展名
查看>>
Linux中的进程
查看>>
学习python(1)——环境与常识
查看>>
学习设计模式(3)——单例模式和类的成员函数中的静态变量的作用域
查看>>