<span style="font-size:14px;"><strong>编程中遇到转码问题,本文是将GBK转换到UTF8的源码,只需要自行改动srcDir和destDir即可。</strong>
package com.ansj.vec;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Updata Time
*/
public class UTF8Parser {
/**
* @author niewj
* @since 2012-6-1
*/
static File srcDir = new File(
"E:/W2V_test/Files_ZhengCe2/");
static File destDir = new File(
"E:/W2V_test/Files_ZhengCe/");
public static void main(String[] args) {
// 1.判断是目录
if (!srcDir.isDirectory()) {
return;
}
// 2.遍历所有目录
File[] fs = srcDir.listFiles();
// 创建目标目录
if (!destDir.exists()) {
destDir.mkdirs();
}
try {
new UTF8Parser().parse(fs);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 目录就迭代遍历;文件就重编码
*
* @author niewj
* @since 2012-6-1
*/
private void parse(File[] fs) throws IOException {
for (File file : fs) {
if (!file.isDirectory()) {
File destFile = new File(destDir, file.getName());
parse2UTF_8(file, destFile);
} else {
parse(file.listFiles());
}
}
}
/**
* @Updata Time 2014/12/11
*/
private void parse2UTF_8(File file, File destFile) throws IOException {
StringBuffer msg = new StringBuffer();
// 读写对象
PrintWriter ps = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(destFile, false), "utf8"));
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "gbk"));
// 读写动作
String line = br.readLine();
while (line != null) {
msg.append(line).append("\r\n");
line = br.readLine();
}
ps.write(msg.toString());
br.close();
ps.flush();
ps.close();
}
}</span>
GBK至UTF8转码
最新推荐文章于 2025-06-10 10:42:03 发布