因为txt默认的选项是ANSI,即GBK编码。GBK和GB2312都是中文编码,先解释一下两者的区别。
总体说来,GBK包括所有的汉字,包括简体和繁体。而gb2312则只包括简体汉字。
GBK编码是中国大陆制订的、等同于UCS的新的中文编码扩展国家标准。GBK工作小组于1995年10月,同年12月完成GBK规范。该编码标准兼容GB2312,共收录汉字21003个、符号883个,并提供1894个造字码位,简、繁体字融于一库。
那么如何查看txt的编码呢,教你一个笨笨的办法:打开文档——另存为,然后看到最下面的编码,默认选择的就是当前文档的编码格式)。
txt文本文档有四种编码选项:ANSI、Unicode、Unicode big endian、UTF-8;默认应该是ANSI选项,就是系统的默认编码,一般是GBK。
因此我们读取txt文档可能有时候并不知道其编码格式,所以需要用进程动态判断获取txt文档编码,这里给一点资料,参考参考:
ANSI: 无格式定义
Unicode: 前两个字节为FFFE Unicode文档以0xFFFE开头
Unicode big endian: 前两字节为FEFF
UTF-8: 前两字节为EFBB UTF-8以0xEFBBBF开头
用进程取出前几个字节并进行判断即可。
首先对java中得编码格式进行了研究。发现在java中
java编码与txt编码对应:
javatxtunicodetxt
utf-8utf-8
utf-16unicode
gb2312ANSI
java读取txt文档,如果编码格式不匹配,就会出现乱码现象。所以读取txt文档的时候需要设置读取编码。txt文档编码格式都是写在文档头的,在进程中需要先解析文档的编码格式,获得编码格式后,在按此格式读取文档就不会产生乱码了。
代码示例如下:package com.lfl.attachment;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TextMain {
public static void main(String[] args) throws Exception {
String filePath = "D:/article.txt";
// String filePath = "D:/article333.txt";
// String filePath = "D:/article111.txt";
String content = readTxt(filePath);
System.out.println(content);
}
/**
* 解析普通文本文档 流式文档 如txt
* @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);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("读取文档:" + path + "失败!");
}
return content.toString();
}
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;
}
}