java.nio.charset.MalformedInputException: Input length = 1异常

Charset.forName("gbk");
BufferedReader bufferedReader = Files.newBufferedReader(Paths.get("d://mylog.log"), StandardCharsets.UTF_8);
String line = "";
while ((line = bufferedReader.readLine()) != null){
   System.out.println(line);
}

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
    at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.BufferedReader.fill(BufferedReader.java:161)
    at java.io.BufferedReader.readLine(BufferedReader.java:324)
    at java.io.BufferedReader.readLine(BufferedReader.java:389)
 

后来把要读取的文件编码改为为utf-8编码,就不报异常了。

 

你可能感兴趣的:(异常)