写了一个简易的GBK文件向UTF8文件转换的工具

时间:2022-01-06 14:59:15
package com.bocom.framework.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; public class GBK2UTF8 { /**
* GBK文件转换为UTF-8文件
*
* @throws Exception
*/
public static void convert() throws Exception { File f = new File("D:\\Program Files\\workspace\\Plug_In_Interface_V1\\src\\com\\bocom\\framework\\util");
File files[] = f.listFiles(); for (File file : files) {
if (file.isDirectory()) {
continue;
}
FileInputStream in = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(in, "gbk"));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
reader.close();
file.delete(); FileOutputStream out = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
writer.write(sb.toString());
writer.close();
}
} public static void main(String[] arg) { try {
GBK2UTF8.convert();
} catch (Exception e) {
e.printStackTrace();
}
}
}