Java实现将中文转换为拼音首字母

渡星河
2024-01-30 / 0 评论 / 9 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年01月30日,已超过232天没有更新,若内容或图片失效,请留言反馈。

调用例子

package com.zhkt.ostrich;

/**
 * Author: duGalaxy
 * Date: 2023/09/11/11:03
 * Description:
 */
import java.io.File;

import static com.zhkt.ostrich.ChineseToLower.getPinYinHeadChar;


public class test {
    public static void main(String[] args) {
        String directoryPath = "F:\\A公司项目\\汐游游戏\\20240126汐游二级02切图\\欢乐世界"; // 替换为你的目录路径
        renameFiles(directoryPath);
//        int i = 2;
//        while (i<=20){
//            System.out.println(directoryPath+i);
//            renameFiles(directoryPath+i);
//            i++;
//        }

    }

    public static void renameFiles(String directoryPath) {
        File directory = new File(directoryPath);
        File[] files = directory.listFiles();
        int num = 1;
        String texy = "";
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    String originalName = file.getName();
                    //讲包含病毒的文件名进行文件新命名
                    texy+=originalName+"\n";
                    String headChar = getPinYinHeadChar(originalName);
                    String newName = headChar.toLowerCase();
//                    String newName;
//                    String newName = originalName.replaceAll("欢乐世界", "").replaceAll("大", "").replaceAll("背景", "bg");
//                    newName = originalName.replaceAll(" ", "");
//                    newName = originalName.replaceAll("探险岛", "txd");
//                    String newName = num+".png";
                    num++;
                    if (!originalName.equals(newName)) {
                        String newPath = file.getParent() + File.separator + newName;
                        File newFile = new File(newPath);

                        if (file.renameTo(newFile)) {
                            System.out.println("文件重命名成功:" + originalName + " --> " + newName);
                        } else {
                            System.out.println("文件重命名失败:" + originalName);
                        }
                    }
                }

            }
        }
        System.out.println(texy);
    }
}

工具类


package com.zhkt.ostrich;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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 ChineseToLower {

    /**
     * 得到中文首字母
     * @param str 需要转化的中文字符串
     * @return
     */
    public static String getPinYinHeadChar(String str)
    {
        String convert = "";
        for (int j = 0; j < str.length(); j++)
        {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null)
            {
                convert += pinyinArray[0].charAt(0);
            } else
            {
                convert += word;
            }
        }
        return convert;
    }
    /**
     * 得到中文全拼
     * @param str 需要转化的中文字符串
     * @return
     */
    public static String getPingYin(String str)
    {
        char[] t1 = null;
        t1 = str.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try
        {
            for (int i = 0; i < t0; i++)
            {
                // 判断是否为汉字字符
                if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+"))
                {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else
                {
                    t4 += java.lang.Character.toString(t1[i]);
                }
            }
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1)
        {
            e1.printStackTrace();
        }
        return t4;
    }

    /**
     * 获得中文字符串首字母
     * @param str
     * @return
     */
    public static String getFirstPinYin(String str){
        String s = getPinYinHeadChar(str);
        StringBuffer sb = new StringBuffer(s);
        if (sb.length() > 1)
        {
            String ss = sb.delete(1, sb.length()).toString();
            return String.valueOf(Character.toUpperCase(ss.toCharArray()[0]));
        }
        return str;
    }
    public static void main(String[] args) {
        //获取中文拼音首字母
        String headChar = getPinYinHeadChar("欢乐世界-打我鸭.png");
//        headChar.toUpperCase()
        String up = headChar.toLowerCase();
        System.out.println(up);
        //获得中文拼音全拼
        String pingYin = getPingYin("磊喆頔");
        String s = pingYin.toUpperCase();
        System.out.println(s);
        //获得中文拼音第一个字首字母
        String ss = getFirstPinYin("湖北省");
        System.out.println(ss);


    }
}
0

评论 (0)

取消