需要的依赖
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
代码
可以直接用我已经编译好的jar包
然后直接复制到你需要翻译的文件中(注意,会递归其子文件夹)
接着 执行
java -jar cn-convert-pingying.jar
就可以全部重命名为拼音文件了
假如要还原的话(注意 必须当然文件夹中含有pingying.txt这个文件才能还原,该文件为 翻译备用文件,自动生成)
执行
java -jar cn-convert-pingying.jar -back
就可以还原
package com.xiaoyun.conver;
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.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConvertMain {
public static final HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
public static final Stack<String> stack = new Stack<>();
public static final LinkedList<String> list = new LinkedList<>();
public static final String userDir = System.getProperty("user.dir");
public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination {
if (args.length < 1 || args == null) {
getFileNameConvert();
outFile();
}else {
List<String> commandList = Arrays.asList(args);
for (int i = 0; i < commandList.size(); i++) {
String command = commandList.get(i);
if ("-back".equals(command)) {
inFile();
backCovert();
}
if ("-convert".equals(command)) {
getFileNameConvert();
outFile();
}
}
}
}
private static void backCovert() {
if (list == null || list.size() < 1) {
return;
}
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
String[] strings = str.split(";");
if (strings.length != 3) {
return;
}
String parentFile = new File(strings[2]).getParent();
File file = new File(parentFile, strings[1]);
file.renameTo(new File(parentFile, strings[0]));
}
}
private static void getFileNameConvert() throws BadHanyuPinyinOutputFormatCombination {
//拼音小写
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
//不带声调
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
//要转换的中文,格式,转换之后的拼音的分隔符,遇到不能转换的是否保留 wo,shi,zhong,guo,ren,,hello
File file = new File(userDir);
File[] files = file.listFiles();
if (files != null && files.length > 0) {
listFile(files, stack);
}
}
private static void outFile() {
try (BufferedWriter out = new BufferedWriter(new FileWriter(new File(userDir, "pingying.txt")))) {
int size = stack.size();
for (int i = 0; i < size; i++) {
out.write(stack.pop());
out.newLine();
}
System.out.println("翻译创建成功!");
} catch (IOException e) {
}
}
private static void inFile(){
try (BufferedReader br = new BufferedReader(new FileReader(new File(userDir, "pingying.txt")))) {
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
} catch (FileNotFoundException ex) {
System.out.println("当然文件夹不存在pingying.txt");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void listFile(File[] files, Stack<String> list) throws BadHanyuPinyinOutputFormatCombination {
for (File source : files) {
System.out.println("当前目录: " + source.getAbsolutePath());
if (source.isDirectory()) {
listFile(source.listFiles(), list);
}
convert(list, source);
}
}
private static void convert(Stack<String> list, File source) throws BadHanyuPinyinOutputFormatCombination {
String name = source.getName();
if (isContainChinese(name)) {
String target = PinyinHelper.toHanYuPinyinString(name, format, "", true);
source.renameTo(new File(source.getParent(), target));
System.out.println("source: " + name + " ---- target: " + target);
list.push(name + ";" + target + ";" + source.getAbsolutePath());
}
}
/**
* 判断字符串中是否包含中文
*
* @param str 待校验字符串
* @return 是否为中文
* @warn 不能校验是否为中文标点符号
*/
public static boolean isContainChinese(String str) {
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
if (m.find()) {
return true;
}
return false;
}
}