Java项目GBK编码转UTF-8

时间:2023-01-05 10:19:05

方法一:使用Apache的common-io

1.maven项目pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>

2.代码
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import org.apache.commons.io.FileUtils;

public class Main {
public static void main(String[] args) {
//GBK编码格式源码路径
String srcDirPath = "D:\\ENews\\src";
//转为UTF-8编码格式源码路径
String utf8DirPath = "D:\\ENews\\UTF8\\src";
//获取所有java文件
Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[]{"java"}, true);
for (File javaGbkFile : javaGbkFileCol) {
//UTF8格式文件路径
String utf8FilePath = utf8DirPath+javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
//使用GBK读取数据,然后用UTF-8写入数据
try {
FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

方法二:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ConverEncoding {

public static void main(String[] args) throws Exception {
String srcDir = "E:\\project\\ReadFile\\src";
List<String> files = new ArrayList<String>();
fetchFileList(srcDir, files, ".java");
for(String fileName : files){
convert(fileName, "GBK", fileName, "UTF-8");
}
}
public static void convert(String oldFile, String oldCharset, String newFlie, String newCharset){
BufferedReader bin;
FileOutputStream fos;
StringBuffer content = new StringBuffer();
try {
System.out.println(oldFile);
bin = new BufferedReader(new InputStreamReader(new FileInputStream(oldFile), "gbk"));
String line = null;
while((line=bin.readLine())!=null){
content.append(line);
content.append(System.getProperty("line.separator"));
}
bin.close();
File dir = new File(newFlie.substring(0, newFlie.lastIndexOf("\\")));
if(!dir.exists()){
dir.mkdirs();
}
fos = new FileOutputStream(newFlie);
Writer out = new OutputStreamWriter(fos, newCharset);
out.write(content.toString());
out.close();
fos.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fetchFileList(String strPath, List<String> filelist, final String regex) {
File dir = new File(strPath);
File[] files = dir.listFiles();
Pattern p = Pattern.compile(regex);
if (files == null)
return;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
fetchFileList(files[i].getAbsolutePath(), filelist, regex);
} else {
String strFileName = files[i].getAbsolutePath().toLowerCase();
Matcher m = p.matcher(strFileName);
if(m.find()){
filelist.add(strFileName);
}
}
}
}
}