一,设置编码格式
1.preferences->workspace->text file encoding->选择utf-8
2.preferences->content type->text 自己手动写入utf-8
一般来说就可以了
二,还是不行怎么办,先备份一份工程(一定要备份),运行这份代码即可。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class ConUniToUTF {
/**
* @param args
*/
public static void main(String[] args) {
try {
String path = "E:\\javaworkspace\\test"; //要转码的项目目录
recursiveFiles(path);// 获得该目录下的文件路径
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void recursiveFiles(String path) {
File file = new File(path);
// 如果这个路径是文件夹
if (file.isDirectory()) {
// 获取路径下的所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
// 如果还是文件夹 递归获取里面的文件 文件夹
if (files[i].isDirectory()) {
recursiveFiles(files[i].getPath());
} else {
convertFile(files[i].getPath());
}
}
} else {
convertFile(file.getPath());
}
}
private static void convertFile(String filepath) {
String endName = filepath.substring(filepath.lastIndexOf(".") + 1);
if ("java".equalsIgnoreCase(endName) || "text".equalsIgnoreCase(endName)
|| "lrc".equalsIgnoreCase(endName)) {
System.out.println(">>>>>>>>>>" + filepath);
printUTFTxt(filepath);// 打印
}
}
public static void printUTFTxt(String filePath) {
try {
String content = readTxt(filePath);
File writename = new File(filePath);
// 相对路径,如果没有则要建立一个新的output。txt文件
writename.createNewFile(); // 创建新文件
BufferedWriter out = new BufferedWriter(new FileWriter(writename));
out.write(content); // \r\n即为换行
out.flush(); // 把缓存区内容压入文件
out.close(); // 最后记得关闭文件
} catch (Exception e) {
}
}
/**
* 获得文件夹下的文件路径
*
* @param path
* @return
*/
private static List<String> getFile(String path) {
List<String> l = new ArrayList<String>();
File file = new File(path);
File[] array = file.listFiles();
for (int i = 0; i < array.length; i++) {
if (array[i].isFile()) {
System.out.println("*****" + array[i].getPath());
l.add(array[i].getPath());
}
}
return l;
}
/**
* 解析普通文本文件 流式文件 如txt http://lfl2011.iteye.com/blog/1930107 此人博客
*
* @param path
* @return
*/
@SuppressWarnings("unused")
public static String readTxt(String path) {
StringBuilder content = new StringBuilder("");
try {
String code = resolveCode(path);
File file = new File(path);
InputStream is = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(is, code);
BufferedReader br = new BufferedReader(isr);
// char[] buf = new char[1024];
// int i = br.read(buf);
// String s= new String(buf);
// System.out.println(s);
String str = "";
while (null != (str = br.readLine())) {
content.append(str + "\r\n");
}
br.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("读取文件:" + path + "失败!");
}
return content.toString();
}
/**
*
* @param path
* @return
* @throws Exception
*/
public static String resolveCode(String path) throws Exception {
// String filePath = "D:/article.txt"; //[-76, -85, -71] ANSI
// String filePath = "D:/article111.txt"; //[-2, -1, 79] unicode big
// endian
// String filePath = "D:/article222.txt"; //[-1, -2, 32] unicode
// String filePath = "D:/article333.txt"; //[-17, -69, -65] UTF-8
InputStream inputStream = new FileInputStream(path);
byte[] head = new byte[3];
inputStream.read(head);
String code = "gb2312"; // 或GBK
if (head[0] == -1 && head[1] == -2)
code = "UTF-16";
else if (head[0] == -2 && head[1] == -1)
code = "Unicode";
else if (head[0] == -17 && head[1] == -69 && head[2] == -65)
code = "UTF-8";
inputStream.close();
System.out.println(code);
return code;
}
}