1. Look at the http://pinyin4j.sourceforge.net here/
2. Download pinyin4j-1.x.x.jar
3. Check the demo.
Import net. sourceforge. pinyin4j. pinyinHelper; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinCaseType; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinOutputFormat; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinToneType; <br/> import net. sourceforge. pinyin4j. format. hanyuPinyinVCharType; <br/>/** <br/> * A demostration to show how does the pinyin4j work <br/> * @ author Li Min (xmlerlimin@gmail.com) <br/> */<br/> public class MainDemo {<br/>/** <br/> * @ param args <br/> */ <br/> public static void main (String [] args) {<br/> // deal with Chinese sentence <br/> {<br/> HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat (); <br/> // defautlt output format <br/> System. out. println ("defautlt output format"); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // with seperater <br/> System. out. println ("with seperater"); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // output uppercase <br/> System. out. println ("output uppercase"); <br/> outputFormat. setCaseType (HanyuPinyinCaseType. UPPERCASE); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // without tone numbers or tone marks <br/> System. out. println ("without tone numbers or tone marks"); <br/> outputFormat. restoreDefault (); <br/> outputFormat. setToneType (HanyuPinyinToneType. WITHOUT_TONE); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("My Chinese heart", outputFormat, ""); <br/> System. out. println ("***"); <br/> // use u: as an alternative of ü' <br/> System. out. println ("use u: as an alternative of ü'"); <br/> outputFormat. restoreDefault (); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("", outputFormat, ""); <br/> System. out. println ("***"); <br/> // use v as an alternative of ü' <br/> System. out. println ("use v as an alternative of ü'"); <br/> outputFormat. setVCharType (HanyuPinyinVCharType. WITH_V); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("", outputFormat, ""); <br/> System. out. println ("***"); <br/> // directly output ü' in Unicode form <br/> System. out. println ("directly output ü' in Unicode form"); <br/> outputFormat. setVCharType (HanyuPinyinVCharType. WITH_U_UNICODE); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("", outputFormat, ""); <br/> System. out. println ("***"); <br/> // deal with combination of Chinese and non-Chinese <br/> System. out. println ("deal with combination of Chinese and non-Chinese"); <br/> outputFormat. restoreDefault (); <br/> System. out. println (PinyinHelper. toHanyuPinyinString ("Dow Jones index rose 5%", outputFormat, ""); <br/> System. out. println ("***"); <br/>}< br/> // deal with Chinese character <br/>{< br/> HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat (); <br/>{< br/> // single pronounciation <br/> String [] pinyinArray = PinyinHelper. toHanyuPinyinStringArray ('my', outputFormat); <br/> System. out. println ("***"); <br/> for (int I = 0; I <pinyinArray. length; I ++) {<br/> StringBuffer stringbuf = new StringBuffer (); <br/> stringbuf. append ("pinyin"); <br/> stringbuf. append (I); <br/> stringbuf. append (":"); <br/> stringbuf. append (pinyinArray [I]); <br/> System. out. println (stringbuf. toString (); <br/>}< br/> System. out. println ("***"); <br/>}< br/>{< br/> // multiple pronounciations <br/> String [] pinyinArray = PinyinHelper. tohanyupinypolicingarray ('中', outputFormat); <br/> System. out. println ("***"); <br/> for (int I = 0; I <pinyinArray. length; I ++) {<br/> StringBuffer stringbuf = new StringBuffer (); <br/> stringbuf. append ("pinyin"); <br/> stringbuf. append (I); <br/> stringbuf. append (":"); <br/> stringbuf. append (pinyinArray [I]); <br/> System. out. println (stringbuf. toString (); <br/>}< br/> System. out. println ("***"); <br/>}< br/>}