Java 将中文文件名称修改为拼音首字母、拼音全称
正文
在日常工作中,由于一些软件导入文件时对中文不支持,需要修改文件名称为拼音或者拼音首字母,修改方式也多种多样,本文通过Java来进行修改。
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.1</version>
</dependency>
主要通过pinyin4j进行处理。
/**
* 获取汉字串拼音首字母,英文字符不变
*
* @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().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();
}
使用Hutool的IOUtil、FileUtil
工具类对文件进行复制。
public static void main(String[] args) {
//1、待转换文件所在文件夹
File file = new File("E:\\needConvert\\");
//2、转换之后文件生成的文件夹位置
File filecopy = new File("E:\\converted\\");
if (!filecopy.exists()) {
filecopy.mkdirs();
}
BufferedInputStream in = null;
BufferedOutputStream out = null;
File[] filelist = file.listFiles();
for (File fi : filelist) {
//1、获取到文件名
String substring = fi.getName().substring(0, fi.getName().indexOf('.'));
//2、文件名中文转换为拼音首字母
String newPrefix = getFirstSpell(substring);
//3、获取文件的后缀
String suffix = fi.getName().substring(fi.getName().lastIndexOf('.'));
//4、将需要处理的文件输入到流中
in = FileUtil.getInputStream(fi);
//5、新文件的名称及地址:filecopy + newPrefix + suffix
String newFilePath = filecopy + File.separator + newPrefix + suffix;
File newfile = new File(newFilePath);
if (!newfile.exists()) {
try {
newfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
out = FileUtil.getOutputStream(newFilePath);
//6、使用Hutool进行复制
long copyTime = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE);
System.out.printf("转换耗时: %d \n", copyTime);
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
代码地址: https://github.com/copyers/JavaUtils.
以上就是通过Java来转换中文的过程,不过个人觉得批量修改文件名可以通过脚本来遍历修改,更加方便快捷。