Home  >  Q&A  >  body text

java8 - java.nio.charset.MalformedInputException: Input length = 2

大家讲道理大家讲道理2766 days ago1506

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-18 10:47:13

    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

    reply
    0
  • PHPz

    PHPz2017-04-18 10:47:13

    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.

    reply
    0
  • Cancelreply