java8 - java.nio.charset.MalformedInputException: Input length = 2
问题描述
问题解答
回答1:public static void main(String[] args) throws IOException {File file = new File('/home/yangxiaohuan/Documents/TokenizeThenSplitParallelDeletePatternLTZero.txt');InputStreamReader read = new InputStreamReader(new FileInputStream(file), 'UTF-8');// 考虑到编码格式BufferedReader br = new BufferedReader(read);int cnt=0;while(br.ready()){ String text = br.readLine(); cnt++; if(cnt>=47334){System.out.println(text);} System.out.println('cnt = '+cnt); }}}
原来的代码需要导入下面的类import java.nio.file.Files;import java.nio.file.Paths;但是换成这种形式import java.io.File;import java.io.FileInputStream;就是正常的,没有出错。而且还有朋友说,将原来的文本里面,将出错的那一行,删去一个字符也是不会报错的。很奇怪的问题。不知道为啥用nio.file.Files就是有问题的
回答2:谢邀。你可能搞错了IO和NIO之间的区别,最基本的一点是IO是面向流的,NIO面向缓冲区的,而你的代码很明显是使用了BufferedReader以及InputStreamReader流,如果使用nio,readLine根本不能读取,NIO只能读取缓冲区,扫描缓冲区的大小,并且在解析数据时,比起阻塞IO流,NIO需要付出更大的代价。
相关文章:
![基于Nginx的Wordpress安装失败?](http://www.haobala.com/attached/image/news/202208/112305819e.png)