NutzCN Logo
问答 读取文件乱码问题之二
发布于 3100天前 作者 qq_74967e40 1656 次浏览 复制 上一个帖子 下一个帖子
标签: 乱码

我有一个文件,里面有多行,且带有汉字,文件本身是ASNI编码的(GBK?)
我用Nutz和Java原生的方法去读取,代码如下:
----------------------Java-------------------------------

File f...
BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(f), "GBK")); 
String str = null;
while((str = buf.readLine()) != null){ 
  String l = new String(str.getBytes(),"UTF-8");
  System.out.println(l);//这里是正确的
}
buf.close();

------------------Nutz-------------------------------------

List<String> ls = Files.readLines(f);
ls.forEach(str->{
String l = null;
try {
	l = new String(str.getBytes(),"UTF-8");
} catch (Exception e) {
	e.printStackTrace();
}
	System.out.println(l);//这里就是乱码了。。
});

何解?

4 回复

不能编辑。。

BufferedReader buf = new BufferedReader(new InputStreamReader(new FileInputStream(f), "GBK")); 
String str = null; 
while((str = buf.readLine()) != null){ 
	String l = new String(str.getBytes(),"UTF-8"); 
	System.out.println(l);//这里是正确的 
}
buf.close(); 

暂时无解, Files.readLines暂时还不能带编码,自己写吧

添加回复
请先登陆
回到顶部