ANSI编码的TXT文本乱码
如下图所示,这些TXT是ANSI编码的,用记事本打开,然后另存为就能看到编码。
如果用如下代码就是乱码输出,
String filePath = "E:\\study\\小说\\卫斯理全集\\卫斯理全集1-30\\卫斯理001(钻石花).txt";
File file = new File(filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
String content = br.readLine();
需要指定读取时编码,当然,如果TXT不是ansi,那么就不能使用gb2312读取了。
String filePath = "E:\\study\\小说\\卫斯理全集\\卫斯理全集1-30\\卫斯理001(钻石花).txt";
File file = new File(filePath);
BufferedReader br = new BufferedReader(new FileReader(file));
//start 解决txt是ansi编码格式
FileInputStream fis = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(fis,"GB2312");//ansi编码时
br = new BufferedReader(isr);
//end
String content = br.readLine();