这是一个完整可运行的Java类(自行修改文件地址即可):
package com.glaway.bean;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class UnicodeToUtf8 {
private static String outPath = "";
public static void main(String[] args) throws IOException {
//指定要转码文件路径
outPath="E:\\++转码后文件夹";
//开始转码并输出文件
getFiles("E:\\转码前文件夹");
System.out.println("文件转码完成!");
}
/**
* 获取当前目录下所有文件(包含子文件夹)并转码输出
* @param path
* @throws IOException
*/
public static void getFiles(String path) throws IOException {
File file = new File(path);
// 如果这个路径是文件夹
if (file.isDirectory()) {
// 获取路径下的所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
// 如果还是文件夹 递归获取里面的文件 文件夹
if (files[i].isDirectory()) {
getFiles(files[i].getPath());
} else {
writerToFile(file.getPath(),files[i].getPath());
}
}
}
}
/**
* 文件进行读入并转码输出
* @param fileDir
* @throws IOException
*/
public static void writerToFile(String wjDri,String fileDir) throws IOException{
String newWjDri = getNewDir(wjDri);
String newFileDir = getNewDir(fileDir);
File file = new File(newWjDri);
if(!file.exists()){
//先得到文件的上级目录,并创建上级目录,在创建文件
file.mkdirs();
}
File file2 = new File(newFileDir);
FileWriter fw = new FileWriter(file2);
BufferedWriter bw=new BufferedWriter(fw);
FileReader fr = new FileReader(new File(fileDir));
BufferedReader br = new BufferedReader(fr);
String line = "";
while((line = br.readLine()) != null){
//当前行转码
String utf8Str = toUtf8(line);
bw.write(utf8Str+"\t\n");
}
br.close();
fr.close();
bw.close();
fw.close();
}
/**
* 获取转码后文件位置
* @param dir
* @return
*/
public static String getNewDir(String dir){
String[] splits = dir.split("C:");
if(splits.length>1){
dir = outPath+splits[1];
}
return dir;
}
/**
* 字符串转码Unicode to Utf-8
* @param lineStr
* @return
* @throws UnsupportedEncodingException
*/
public static String toUtf8(String lineStr) throws UnsupportedEncodingException{
String str = new String(lineStr.getBytes(), "UTF-8");
char aChar;
int len = str.length();
StringBuffer outBuffer = new StringBuffer(len);
for (int x = 0; x < len;) {
aChar = str.charAt(x++);
if (aChar == '\\') {
aChar = str.charAt(x++);
if (aChar == 'u') {
int value = 0;
for (int i = 0; i < 4; i++) {
aChar = str.charAt(x++);
switch (aChar) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = (value << 4) + aChar - '0';
break;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
value = (value << 4) + 10 + aChar - 'a';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = (value << 4) + 10 + aChar - 'A';
break;
default:
throw new IllegalArgumentException(
"Malformed \\uxxxx encoding.");
}
}
outBuffer.append((char) value);
} else {
if (aChar == 't')
aChar = '\t';
else if (aChar == 'r')
aChar = '\r';
else if (aChar == 'n')
aChar = '\n';
else if (aChar == 'f')
aChar = '\f';
outBuffer.append(aChar);
}
} else
outBuffer.append(aChar);
}
return outBuffer.toString();
}
}