1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 把中文目录名称修改成英文首字母大写或中文拼音目录

把中文目录名称修改成英文首字母大写或中文拼音目录

时间:2022-01-04 19:02:31

相关推荐

把中文目录名称修改成英文首字母大写或中文拼音目录

把中文目录名称修改成英文首字母大写或英文目录

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.6.3</version><scope>compile</scope></dependency><dependency><groupId>com.belerweb</groupId><artifactId>pinyin4j</artifactId><version>2.5.1</version></dependency>

package com.xxx.page;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.File;import java.util.*;public class test02 {/** 存放当前目录的名称 查重用*/public static Map<String,Integer> map = new HashMap<>();/** 存放当前目录名称*/public static String currentName = "";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().toUpperCase();}/*** 获取汉字串拼音,英文字符不变** @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 changeName(String name){if (map.containsKey(name)) {int index = map.get(name);currentName = name + "_" + index;map.put(name,index + 1);}else {currentName = name;map.put(name,1);}}/*** @Author: CS* @Description: 获取当前目录名称*/public static String getNamePath(String name){changeName(name);return currentName;}/*** 转换当前得文件夹 把中文目录变成英文* @param path*/public static void renamePath(String path){//存放当前 目录的集合//1、待转换文件所在文件夹File file = new File(path);File[] filelist = file.listFiles();for (File fi : filelist) {//1、获取到文件名if (fi.isDirectory()){String substring = fi.getName();String newPrefix = getFirstSpell(substring);//3、获取文件的后缀System.out.println(fi.getParent());System.out.println(fi.getPath());String newPath = fi.getParent() + "\\" + newPrefix;newPath = getNamePath(newPath);fi.renameTo(new java.io.File(newPath));renamePath(newPath);}}}public static void main(String[] args) {renamePath("Z:\\test\\english_file");// 测试目录同名遍历// String[] str = new String[]{"hd","hd","hd","hd"};// for (int i = 0; i < str.length; i++) {// changeName(str[i]);// System.out.println(currentName);// }}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。