1. 程式人生 > 實用技巧 >pinyin4j 實現 中文和拼音之間轉化

pinyin4j 實現 中文和拼音之間轉化

maven依賴:
        <!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.0</version>
        </dependency>

import lombok.extern
.slf4j.Slf4j; 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.exception.BadHanyuPinyinOutputFormatCombination; import java.io.
*; /** * @Date 2020/7/2 14:08 */ @Slf4j public class ChineseToEnglish { /** * 獲取漢字串拼音首字母,英文字元不變 * * @param chinese 漢字串 * @return 漢語拼音首字母 */ public static String getFirstSpell(String chinese) { StringBuffer pybf = new StringBuffer(); char[] arr = chinese.toCharArray(); HanyuPinyinOutputFormat defaultFormat
= new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < arr.length; i++) { if (arr[i] > 128) { try { String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat); if (temp != null) { pybf.append(temp[0].charAt(0)); } } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { pybf.append(arr[i]); } } return pybf.toString().replaceAll("\\W", "").trim(); } /** * 獲取漢字串拼音,英文字元不變 * * @param chinese 漢字串 * @return 漢語拼音 */ public static String getFullSpell(String chinese) { StringBuffer pybf = new StringBuffer(); char[] arr = chinese.toCharArray(); HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat(); defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE); defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE); for (int i = 0; i < arr.length; i++) { if (arr[i] > 128) { try { pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[0]); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } else { pybf.append(arr[i]); } } return pybf.toString(); } public static void main(String[] args) {
//讀取檔案的位置 File pathFile
= new File("H:\\weixindownload\\WeChat Files\\wxid_h4rkelzyxk5t22\\FileStorage\\File\\2020-07\\不停電次數(1)(1)(1).xls"); if (pathFile.exists()) { System.out.println("exists"); InputStreamReader inputStreamReader = null; try { inputStreamReader = new InputStreamReader(new FileInputStream(pathFile), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String LineTxt = null; while ((LineTxt = bufferedReader.readLine()) != null) { String[] split = LineTxt.split(","); for (int i = 0; i <= 43; i++) { System.out.println("/** " + split[i] + " */"); System.out.println("@ExcelProperty(\"" + getFirstSpell(split[i]) + "\")"); System.out.println("private String " + getFirstSpell(split[i]) + ";"); } //在這地方打個斷點 System.out.println("123"); } } catch (UnsupportedEncodingException | FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { System.err.println("no exists"); } } }