读取.dat文件 中文是乱码 该怎么解决

ADC_VN 2014-07-28 10:12:55
先说一下我的情况。
dat文件700多M,包含很多的网页。我的程序如下:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;


public class Reader {

public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
encoding= getFilecharset(file);

if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}

}
private static String getFilecharset(File sourceFile) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1) {
return charset; //文件编码为 ANSI
} else if (first3Bytes[0] == (byte) 0xFF
&& first3Bytes[1] == (byte) 0xFE) {
charset = "UTF-16LE"; //文件编码为 Unicode
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
&& first3Bytes[1] == (byte) 0xFF) {
charset = "UTF-16BE"; //文件编码为 Unicode big endian
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
&& first3Bytes[1] == (byte) 0xBB
&& first3Bytes[2] == (byte) 0xBF) {
charset = "UTF-8"; //文件编码为 UTF-8
checked = true;
}
bis.reset();
if (!checked) {
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read >= 0xF0)
break;
if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
// (0x80
// - 0xBF),也可能在GB编码内
continue;
else
break;
} else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF) {
charset = "UTF-8";
break;
} else
break;
} else
break;
}
}
}
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
return charset;
}


public static void main(String argv[]){
String filePath = "E:\\dataset_604494\\604494\\cwt_quark_70thousand.dat";
// "res/";
readTxtFile(filePath);
}
}


部分运行结果如下:

url: http://yule.sohu.com/20071207/n253862872.shtml
date: Sun, 06 Jan 2008 07:17:40 GMT
ip: 162.105.146.2
unzip-length: 120233
length: 28392

x滌介w堑8鴻:G?姢I葚I婔p嶕灱<IN騸巼??l@癶雹3哝蠣??刷D堚撅獺奣鋆r靫枫尿?uk楫薨壥媑?I牷曛蓐汁[U窞<~黋嘾??
宵缇鎄4姦獜煄噅匱鑄恃濌夓鹵X?$?H^?9較挥楂輓椵??宝茷寀ы乆O璤蕮縊噧лk?v急镠??秀蓍*At ?G?筴%WFS趋齔N湲丳<U?
膫醜拙f9???盚#煪柣B鯍S躻,?J?q(僪q賋>↑筯鳷呅秝?庛荖痌掲??|鞓袡堳銫0&
ぢ県?埬挕}G鋎)刜g84#K?降N&T<?%-0犄:序L篑?禪
...全文
3211 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ADC_VN 2014-07-28
  • 打赏
  • 举报
回复
引用 1 楼 xmt1139057136 的回复:
你换一种编码,utf-8试一试
中文全是问号
业余草 2014-07-28
  • 打赏
  • 举报
回复
你换一种编码,utf-8试一试

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧