前几天写的,因为最开始没有问清楚,结果公司的文件全部是用UTF-8的编码,而我用的GBK,郁闷,那么多文件,还要再转,问题是转换的时间还不能直接把文件编码给换了,那样会出现乱码的情况,只好去网上找工具,找啊找,找到了,结果发现一批量转换就报错,闷.- -# 只好自己动手写了个一个,正好昨天有空,把主要代码整理出来,然后用Swing画了个界面,简单写了个,以后就能直接用了.:)
名称:文件编码批量转换器
功能:可以批量转换某一指定文件夹下的指定后缀的所有文件编码,编码为utf-8,gb2312,gbk之间的转换
说明:不保证转换后出现乱码,因为我测试的全部是正常转换,没有出现问题,还有就是,没有测试输入和输出目录相同的时候会出现啥问题.:(,所以,尽管不会对源文件进行修改,但是还是建议先备份原文件,以免发生不必要的问题.
文件下载:
主要代码:
package com.hmilyld.frame.tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import javax.swing.JTextArea;
public class ConvertStr {
private String NEW_FOLDER;// 需要保存的文件路径
private String FILE_PATH;// 原文件路径
private String oldEncode;// 原编码
private String newEncode;// 转换后的编码
private String houzhui;// 文件后缀名称
private JTextArea view;
public ConvertStr(String filePath, String newFolder, String oldEncode,
String newEncode, String houzhui, JTextArea view) {
this.NEW_FOLDER = newFolder;
this.FILE_PATH = filePath;
this.oldEncode = oldEncode;
this.newEncode = newEncode;
this.houzhui = houzhui;
this.view = view;
}
/**
* 查找出所有文件
*
* @param path
* @throws Exception
*/
public void listFile(String path) throws Exception {
File file = new File(path);
File[] f = file.listFiles();
for (int i = 0; i < f.length; i++) {
if (f[i].isDirectory()) {
// 如果为目录,则再次遍历该目录
this.listFile(f[i].getPath());
} else {
// 如果为文件,则判断是否为指定后缀名称
String name = f[i].getName();
if (name.substring(name.length() - houzhui.length(),
name.length()).equals(houzhui)) {
this.convertFile(path, name);
}
}
}
}
/**
* 转换编码,并写入新文件
*
* @param filePath
* @param fileName
* @throws Exception
*/
private void convertFile(String filePath, String fileName) throws Exception {
filePath = filePath + "\\";
// 重写FileReader类,让file可以以指定编码格式读入
FileReaderByEncode file = new FileReaderByEncode(filePath + fileName,
oldEncode);
BufferedReader br = new BufferedReader(file);
String temp = "";
StringBuffer sb = new StringBuffer();
while ((temp = br.readLine()) != null) {
sb.append(temp + '\n');
}
br.close();
temp = sb.toString();
filePath = filePath.replace(FILE_PATH, NEW_FOLDER);
File tempFile = new File(filePath);
// 检查指定目录中是否存在目录,没有则新建
if (!tempFile.exists()) {
tempFile.mkdirs();
}
// 按照编码写出该文件
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
filePath + fileName), newEncode);
out.write(temp);
out.flush();
out.close();
view.setText(filePath + fileName + " 转换完成" + '\n' + view.getText());
}
}
private String NEW_FOLDER;// 需要保存的文件路径
private String FILE_PATH;// 原文件路径
private String oldEncode;// 原编码
private String newEncode;// 转换后的编码
private String houzhui;// 文件后缀名称
private JTextArea view;
public ConvertStr(String filePath, String newFolder, String oldEncode,
String newEncode, String houzhui, JTextArea view) {
this.NEW_FOLDER = newFolder;
this.FILE_PATH = filePath;
this.oldEncode = oldEncode;
this.newEncode = newEncode;
this.houzhui = houzhui;
this.view = view;
}
/**
* 查找出所有文件
*
* @param path
* @throws Exception
*/
public void listFile(String path) throws Exception {
File file = new File(path);
File[] f = file.listFiles();
for (int i = 0; i < f.length; i++) {
if (f[i].isDirectory()) {
// 如果为目录,则再次遍历该目录
this.listFile(f[i].getPath());
} else {
// 如果为文件,则判断是否为指定后缀名称
String name = f[i].getName();
if (name.substring(name.length() - houzhui.length(),
name.length()).equals(houzhui)) {
this.convertFile(path, name);
}
}
}
}
/**
* 转换编码,并写入新文件
*
* @param filePath
* @param fileName
* @throws Exception
*/
private void convertFile(String filePath, String fileName) throws Exception {
filePath = filePath + "\\";
// 重写FileReader类,让file可以以指定编码格式读入
FileReaderByEncode file = new FileReaderByEncode(filePath + fileName,
oldEncode);
BufferedReader br = new BufferedReader(file);
String temp = "";
StringBuffer sb = new StringBuffer();
while ((temp = br.readLine()) != null) {
sb.append(temp + '\n');
}
br.close();
temp = sb.toString();
filePath = filePath.replace(FILE_PATH, NEW_FOLDER);
File tempFile = new File(filePath);
// 检查指定目录中是否存在目录,没有则新建
if (!tempFile.exists()) {
tempFile.mkdirs();
}
// 按照编码写出该文件
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
filePath + fileName), newEncode);
out.write(temp);
out.flush();
out.close();
view.setText(filePath + fileName + " 转换完成" + '\n' + view.getText());
}
}