简单实现汉字向拼音的转换
2012-08-06 16:13
357 查看
public class PinyinHelper { /** * @param value * 要转换成拼音的汉字 * @return 转换之后的字符串(拼音)数组 * @throws IOException */ public static String[] toPinyin(String value) { String[] pinyinArray = new String[value.length()]; Map<String, String> map = toMap(); // 读取value,得到对应的unicdoe值 for (int i = 0; i < value.length(); i++) { Character character = value.charAt(i); // 转换成unicode String unicode = Integer.toHexString(character); unicode = unicode.toUpperCase();// 因为字库文件中是大写的 String pinyin = map.get(unicode); pinyinArray[i] = pinyin; } return pinyinArray; } private static Map<String, String> toMap() { String line;// 用来保存每行读取的内容 Map<String, String> map = new HashMap<String, String>(); BufferedReader bufferedReader = null; try { InputStream is = PinyinHelper.class .getResourceAsStream("/pingyindb/unicode_to_hanyu_pinyin.txt");//读取jar包下文件的方法 bufferedReader = new BufferedReader(new InputStreamReader(is)); line = bufferedReader.readLine(); while (line != null) { /** * 可以用正则表达式简化 */ String unicode = line.substring(0, 4); String pinyin = line.substring(6, line.length() - 1); map.put(unicode, pinyin); line = bufferedReader.readLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } return map; } public static void main(String[] args) { String[] s = toPinyin("今天天气不错啊!"); for (String temps : s) { System.out.println(temps); } } }
相关文章推荐
- Mysql中文汉字转拼音的实现(每个汉字转换全拼)
- iOS_CoreFoundation_汉字转换拼音的实现
- Java汉字转换成拼音利用pinyin4j的jar包实现
- Mysql中文汉字转拼音的实现(每个汉字转换全拼)
- C#实现汉字转换为拼音缩写的代码
- PHP实现的汉字拼音转换和公历农历转换类及使用示例
- C#实现汉字转换为拼音缩写的代码
- Mysql中文汉字转拼音的实现(每个汉字转换全拼)
- asp.net 汉字转换拼音及首字母实现代码
- 汉字转换成Unicode,简单的java实现方法
- java中将汉字转换成拼音的实现代码
- C#实现汉字转换为拼音缩写的代码
- Java中将中文姓名转换为拼音的简单实现
- 用c#实现把汉字转换成拼音
- 基于jQuery实现汉字转换成拼音代码
- Mysql中文汉字转拼音的实现(每个汉字转换全拼)
- Java实现汉字转换为拼音
- asp.net 汉字转换拼音及首字母实现代码
- Java实现汉字转换为拼音
- C#实现汉字转换为拼音缩写的代码