java8 - java.nio.charset.MalformedInputException: Input length = 2
大家讲道理
大家讲道理 2017-04-18 10:45:13
0
2
1505
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
小葫芦
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);
            }
        }
}

The original code needs to import the following classes
import java.nio.file.Files;
import java.nio.file.Paths;
But change it to this form
import java.io.File;
import java.io. FileInputStream;
is normal and there is no error.
And some friends said that if you delete one character from the original text and the line with the error, there will be no error. Very strange question. I don’t know why there is a problem using nio.file.Files

PHPzhong

Thanks for the invitation. You may have misunderstood the difference between IO and NIO. The most basic point is that IO is stream-oriented, and NIO is buffer-oriented. Your code obviously uses BufferedReader and InputStreamReader streams. If you use nio, readLine will not work at all. Cannot read, NIO can only read the buffer, scan the size of the buffer, and when parsing the data, NIO needs to pay a greater price than blocking the IO stream.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template