使用pinyin4j.jar即可。
package com.wangzhu.pinyin; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; public class Pinyin { public static void main(String[] args) { Pinyin pinyin = new Pinyin(); System.out.println(pinyin.getPinyin("好呀!呵呵!Welcome to .")); System.out.println(pinyin.getPinyinHeadChar("好呀!呵呵!Welcome to .")); } /** * 返回中文拼音 * * @param hanzi * 中文 * @return 中文拼音 */ private String getPinyin(String hanzi) { StringBuffer pinyin = new StringBuffer(); char[] hanziChar = hanzi.toCharArray(); HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat(); pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V); try { for (int i = 0, len = hanziChar.length; i < len; i++) { if (Character.toString(hanziChar[i]).matches( "[\\u4E00-\\u9FA5]+")) { String[] tempArr = PinyinHelper.toHanyuPinyinStringArray( hanziChar[i], pinyinOutputFormat); pinyin.append(tempArr[0]); } else { pinyin.append(Character.toString(hanziChar[i])); } } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } return pinyin.toString(); } /** * 返回中文拼音的首字母 * * @param hanzi * 中文 * @return 中文拼音首字母 */ protected String getPinyinHeadChar(String hanzi) { StringBuffer pinyinHead = new StringBuffer(); for (int i = 0, len = hanzi.length(); i < len; i++) { char tempChar = hanzi.charAt(i); String[] tempArr = PinyinHelper.toHanyuPinyinStringArray(tempChar); if (null != tempArr) { pinyinHead.append(tempArr[0].charAt(0)); } else { pinyinHead.append(tempChar); } } return pinyinHead.toString(); } }
结果:
haoya!hehe!Welcome to .
hy!hh!Welcome to .
以下是反射机制的调用:
package com.wangzhu.reflect; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class TextReflect { /** * @param args * @throws ClassNotFoundException * @throws NoSuchMethodException * @throws SecurityException * @throws InstantiationException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static void main(String[] args) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException { Class<?> classForName = Class.forName("com.wangzhu.pinyin.Pinyin"); Object obj = classForName.newInstance(); Method[] methods = classForName.getDeclaredMethods(); for (int i = 0, len = methods.length; i < len; i++) { System.out.println(methods[i].getName()); } Method method = classForName.getDeclaredMethod("getPinyin", String.class); method.setAccessible(true);// 只有设置为true,才能访问该私有方法 Object temp = method.invoke(obj, "开始啦!恩雅 !之乎者也一眼在!Welcome the World!@"); System.out.println(temp); } }
结果:
main
getPinyin
getPinyinHeadChar
kaishila!enya !zhihuzheyeyiyanzai!Welcome the World!@