学习了JAVA的I/O流,想起来以前没注意到Eclipse里设置的是GBK,导致后来改为UTF-8时用GBK写的源代码中文乱码,自己用JAVA写了个文件字符编码转换的小工具。
打包好的jar可以直接使用:
Windows:
双击start.bat
Linux:
目录终端下 ./start.sh
根据自己需求修改转码配置文件:
下载地址:
https://sou123.lanzous.com/b0b2oonbg 密码:hk64
源代码:
有什么错误,请在评论区指出,谢谢。
StartConversion:
package bin.tools.view;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import bin.tools.service.IOSvr;
import bin.tools.service.ObjectType;
/**
*
* @ClassName: StartConversion
* @Description: main
* @author Eternal2814 3624480535@qq.com
* @date 2020年5月17日
*
*/
public class StartConversion {
public static void main(String[] args) {
System.out.println("转码不会对原文件进行修改,转码错误删除生成的文件即可。");
FileInputStream inputStream = null;
try {
File file = new File("config.cnf");//配置文件路径
inputStream = new FileInputStream(file);//输入流
Properties properties = new Properties();
properties.load(inputStream);//加载配置文件
String srcPath = (String) properties.get("srcPath");//读取需要转换字符集的文件路径或者文件夹
String destPath = (String) properties.get("destPath");//读取输出转换字符集后的文件路径或者文件夹
String srcCode = (String) properties.get("srcCode");//读取文件初始字符集
String destCode = (String) properties.get("destCode");//读取需要转换的字符集
String trans = (String) properties.get("transform");//读取需要转换的文件类型
String[] transform = trans.split(",");//将需要转换的文件类型由字符串切成字符数组
if ("".equals(srcPath) && "".equals(destPath) && "".equals(srcCode) && "".equals(destCode) && "".equals(trans)) {
System.out.println("配置文件错误!");
}else {
boolean startConversion = startConversion(srcCode, destCode, srcPath, destPath, transform);
String result = (startConversion) ? "全部转换成功,请查看输出路径下的log.txt" : "转换失败,请查看输出路径下的log.txt";
IOSvr.logWirter("将目标路径" + srcPath + " 字符集:" + srcCode + " ---> " + destCode + " " + result);
System.out.println(result);
}
} catch (Exception e) {
}finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @Title: startConversion
* @Description: 递归遍历
* @param srcCode
* @param destCode
* @param srcPath
* @param destPath
* @param transform
* @return
* @return boolean
*/
private static boolean startConversion(String srcCode, String destCode,<