Use 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; P Ublic class pinyin {public static void main (string [] ARGs) {pinyin = new pinyin (); system. Out. println (pinyin. getpinyin ("OK! Haha! Welcome to. "); system. Out. println (pinyin. getpinyinheadchar (" Okay! Haha! Welcome. ");}/*** return Chinese pinyin ** @ Param Hanzi * Chinese * @ return Chinese pinyin */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); P Inyinoutputformat. 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 (badhanyupinyino Utputformatcombination e) {e. printstacktrace ();} return pinyin. tostring ();}/*** returns the first letter of Chinese pinyin ** @ 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 ();}}
Result:
Haoya! Hehe! Welcome.
Hy! Hh! Welcome.
The reflection mechanism is called as follows:
Package COM. wangzhu. reflect; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; public class textreflect {/*** @ Param ARGs * @ throws classnotfoundexception * @ throws exceptions * @ throws securityexception * @ throws instantiationexception * @ throws invocationtargetexception * @ throws arguments * /Public stati C 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 = classforname. getdeclaredmethod ("getpinyin", String. class); method. setaccessible (true); // you can access this private method object temp = method only when it is set to true. in Voke (OBJ, "started! Nya! You are also at a glance! Welcome the world! @ "); System. Out. println (temp );}}
Result:
Main
Getpinyin
Getpinyinheadchar
Kaishila! Enya! Zhihuzheyeyiyanzai! Welcome the world! @