基本常用的中文汉字,和一些生僻字都包含在内。

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class ZHConverter {private Properties charMap = new Properties();private Set conflictingSets  = new HashSet();public static final int TRADITIONAL = 0;public static final int SIMPLIFIED = 1;private static final int NUM_OF_CONVERTERS = 2;private static final ZHConverter[] converters = new ZHConverter[NUM_OF_CONVERTERS];private static final String[]  propertyFiles = new String[2];static {propertyFiles[TRADITIONAL] = "zh2Hant.properties";propertyFiles[SIMPLIFIED] = "zh2Hans.properties";}/**** @param converterType 0 for traditional and 1 for simplified* @return*/public static ZHConverter getInstance(int converterType) {if (converterType >= 0 && converterType < NUM_OF_CONVERTERS) {if (converters[converterType] == null) {synchronized(ZHConverter.class) {if (converters[converterType] == null) {converters[converterType] = new ZHConverter(propertyFiles[converterType]);}}}return converters[converterType];} else {return null;}}public static String convert(String text, int converterType) {ZHConverter instance = getInstance(converterType);return instance.convert(text);}private ZHConverter(String propertyFile) {InputStream is = null;is = getClass().getResourceAsStream(propertyFile);//File propertyFile = new File("C:/Temp/testMDB/TestTranslator/abc.txt");if (is != null) {BufferedReader reader = null;try {reader = new BufferedReader(new InputStreamReader(is));charMap.load(reader);} catch (FileNotFoundException e) {} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (reader != null)reader.close();if (is != null)is.close();} catch (IOException e) {}}}initializeHelper();}private void initializeHelper() {Map stringPossibilities = new HashMap();Iterator iter = charMap.keySet().iterator();while (iter.hasNext()) {String key = (String) iter.next();if (key.length() >= 1) {for (int i = 0; i < (key.length()); i++) {String keySubstring = key.substring(0, i + 1);if (stringPossibilities.containsKey(keySubstring)) {Integer integer = (Integer)(stringPossibilities.get(keySubstring));stringPossibilities.put(keySubstring, new Integer(integer.intValue() + 1));} else {stringPossibilities.put(keySubstring, new Integer(1));}}}}iter = stringPossibilities.keySet().iterator();while (iter.hasNext()) {String key = (String) iter.next();if (((Integer)(stringPossibilities.get(key))).intValue() > 1) {conflictingSets.add(key);}}}public String convert(String in) {StringBuilder outString = new StringBuilder();StringBuilder stackString = new StringBuilder();for (int i = 0; i < in.length(); i++) {char c = in.charAt(i);String key = "" + c;stackString.append(key);if (conflictingSets.contains(stackString.toString())) {} else if (charMap.containsKey(stackString.toString())) {outString.append(charMap.get(stackString.toString()));stackString.setLength(0);} else {CharSequence sequence = stackString.subSequence(0, stackString.length()-1);stackString.delete(0, stackString.length()-1);flushStack(outString, new StringBuilder(sequence));}}flushStack(outString, stackString);return outString.toString();}private void flushStack(StringBuilder outString, StringBuilder stackString) {while (stackString.length() > 0){if (charMap.containsKey(stackString.toString())) {outString.append(charMap.get(stackString.toString()));stackString.setLength(0);} else {outString.append("" + stackString.charAt(0));stackString.delete(0, 1);}}}String parseOneChar(String c) {if (charMap.containsKey(c)) {return (String) charMap.get(c);}return c;}public static void main(String[] args) throws IOException {// 繁体转简体 ZHConverter converter = ZHConverter.getInstance(ZHConverter.SIMPLIFIED);  String simplifiedStr = converter.convert("繁體字轉化");  System.out.println(simplifiedStr);  // 简体转繁体 ZHConverter converter2 = ZHConverter.getInstance(ZHConverter.TRADITIONAL);  String traditionalStr = converter2.convert("繁体字转化");  System.out.println(traditionalStr);  }
}

本文需要使用文字库共两个 百度网盘链接: https://pan.baidu.com/s/1o4l0zTwDwt7ifcv_ITuY8Q 提取码: bafv

java中文转繁体汉字相关推荐

  1. Java 中文转拼音/汉字转拼音, 中文转五笔/汉字转五笔, 下载字典!

    Java 中文转拼音/汉字转拼音, 中文转五笔/汉字转五笔, 下载字典! 源码 CharacterElement.java 源码 Dict.java 输出拼音 输出五笔 输出全部 字典下载地址 源码 ...

  2. java 对象 转为繁体_Java实现将数字转化为繁体汉字表示

    在CSDN论坛上看到了一个帖子,要求用Java实现将数字转化为繁体汉字表示,如: 输入一个数字将其转化为大写,比如输入整型1034,得到"壹仟零叁拾肆". 看了之后感觉挺有意思的, ...

  3. Java中文字符串截取,不能返回半个汉字

    转载请注明来源-作者@loongshawn:http://blog.csdn.net/loongshawn/article/details/62215914,建议读者阅读原文,确保获得完整的信息 1. ...

  4. java 乱码怎么是繁体字_[求助]怎么是乱码,里面好多繁体汉字

    [求助]怎么是乱码,里面好多繁体汉字 函数ReadDat()实现从文件ENG.IN中读取一篇英文文章,存入到字符串数组xx中:请编制函数encryptChar(),按给定的替代关系对数组xx中的所有字 ...

  5. 分析java中文乱码的原因

    在java开发中都能遇到java中文乱码的情况,怎样才能够恰当地选择汉字编码方式并正确地处理汉字的编码呢?希望通过下面的总结的java中文乱码解决方法对遇到过此类问题的朋友有所帮助. 首先,要想解决j ...

  6. 深入Java中文问题及最优解决方法--上(转)

    说明:本文为作者原创,作者联系地址为: josserchai@yahoo.com .由于Java编程中的中文问题是一个老生常谈的问题,在阅读了许多关于Java中文问题解决方法之后,结合作者的编程实践, ...

  7. Java 中文乱码问题

    一.中文问题的来源 计算机最初的操作系统支持的编码是单字节的字符编码,于是,在计算机中一切处理程序最初都是以单字节编码的英文为准进行处理.随着计算机的发展,为了适应世界其它民族的语言(当然包括我们的汉 ...

  8. Java 中文 Unicode 编码转换

    Java作为支持多平台的高级程序设计语言自然要支持多种编码方式才能满足程序设计的需要.但是在处理中文&其他编码之间的转换问题时往往出现各种问题,另程序员大伤脑筋.本文着重阐述了Java中文与U ...

  9. java中文乱码的原因及解决方法

    在java开发中都能遇到java中文乱码的情况,怎样才能够恰当地选择汉字编码方式并正确地处理汉字的编码呢?希望通过下面的总结的java中文乱码解决方法对遇到过此类问题的朋友有所帮助. 首先,要想解决j ...

  10. [转]Java中文处理学习笔记——Hello Unicode

    Java中文处理学习笔记--Hello Unicode 作者: 车东 Email: chedongATbigfoot.com/chedongATchedong.com 写于:2002/07 最后更新: ...

最新文章

  1. tensorflow sigmoid_cross_entropy_with_logits 函数解释
  2. 8. American Friendship 美国式的友谊
  3. 设计模式复习-备忘录模式
  4. adb connect 出现timeout的处理方式
  5. 深度学习目标检测系列:RCNN系列算法图解
  6. 【NLP】机器如何认识文本 ?NLP中的Tokenization方法总结
  7. php找不到gearmanClent类,centos 使用docker搭建Gearman任务分发系统 ,Gearman的安装和使用...
  8. 1004 成绩排名 (20分)
  9. 一步一步学Entity FrameWork 4(1)
  10. JavaScript重难点解析2(立即执行函数IIFE,this关键字)
  11. devexpress 创建窗口句柄时出错_MATLAB函数句柄
  12. 页面加载完后直接弹窗或者跳转页面
  13. 电容触摸屏驱动(Linux驱动开发篇)
  14. 卸载mysql front_完全卸载mysql
  15. 毕业设计总结篇之终结篇——基于android的创意展示平台(混合app)
  16. Matlab编程实现图像放大
  17. Shell小脚本实现一键关机/重启虚拟机
  18. [elasticsearch笔记] Query DSL - percolate
  19. 自己开发基于Web的打印控件,真正免费不是共享
  20. Python练手项目:用中国地图验证四色地理

热门文章

  1. 点扩散函数 PSF(point spread function)
  2. 教你设置技嘉主板bios中文图解
  3. 手心输入法导致 Navicat for MySQL闪退的解决办法
  4. java人民币金额大写_Java人民币金额数字转换成大写
  5. 小猫跳圈-第12届蓝桥杯Scratch省赛3真题第1题
  6. opencv图像灰化_Opencv——彩色图像灰度化的三种算法
  7. origin拟合曲线,并绘制曲线上某一点的切线
  8. 文本去重:I-Match算法
  9. 2022年上半年软件设计师常考概念
  10. dom影像图形成数字地形图_基于MapMatrix的数字正射影像图制作