Java MalformedInput Exception on reading text data
<p>I am trying to read a text file with Java. Initially I had hoped to use <code>Files.lines.forEach()</code> but a <code>MalFormedInputException</code> forced me to experiment with different styles. I now have the following code, which reads the file with two different techniques. The first try succeeds, but the second try fails with a <code>MalformedInputException</code>:</p>

<pre><code>InputStream is = new FileInputStream(fileName);
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);

int lineNo = 1;
while( (String line = br.readLine()) != null) {
    System.out.println("Line " + lineNo + ": " + line);
    lineNo++;
}
br.close();

System.out.println("Try again.");
System.out.println();

AtomicInteger lineNoAtomic = new AtomicInteger(1);
try(Stream&lt;String&gt; linesStream = Files.lines(Paths.get(fileName),StandardCharsets.UTF_8)) {
    linesStream.forEach(line -&gt; {
        System.out.println("Line " + lineNoAtomic.get() + ": " + line);
        lineNoAtomic.incrementAndGet();
    });
}
</code></pre>

<p>The (truncated) output is as follows:</p>

<pre><code>Line 250: ar 2_%D8%A3%D8%A8%D8%B1%D9%8A%D9%84 1 1
Line 251: ar 2_%D8%A3%D9%83%D8%AA%D9%88%D8%A8%D8%B1 1 1
Line 252: ar 2_%D8%AF%D9%8A%D8%B3%D9%85%D8%A8%D8%B1 1 1
Line 253: ar 2_%D8%B0%D9%88_%D8%A7%D9%84%D8%AD%D8%AC%D8%A9 1 1
Line 254: ar 2_%D8%B3%D8%A8%D8%AA%D9%85%D8%A8%D8%B1 2 2
Line 255: ar 2_%D9%81%D8%A8%D8%B1%D8%A7%D9%8A%D8%B1 1 1
Line 256: ar 2_%D9%86%D9%88%D9%81%D9%85%D8%A8%D8%B1 1 1
Line 257: ar 2_%D9%8A%D9%86%D8%A7%D9%8A%D8%B1 2 2
Line 258: ar 3%d8%af%d9%8a_%d8%b3%d8%aa%d9%88%d8%af%d9%8a%d9%88_%d9%85%d8%a7%d9%83%d8%b3 1 1
Line 259: ar 300_(%D9%81%D9%8A%D9%84%D9%85) 1 1
Try again.

Line 250: ar 2_%D8%A3%D8%A8%D8%B1%D9%8A%D9%84 1 1
Line 251: ar 2_%D8%A3%D9%83%D8%AA%D9%88%D8%A8%D8%B1 1 1
Line 252: ar 2_%D8%AF%D9%8A%D8%B3%D9%85%D8%A8%D8%B1 1 1
Line 253: ar 2_%D8%B0%D9%88_%D8%A7%D9%84%D8%AD%D8%AC%D8%A9 1 1
Line 254: ar 2_%D8%B3%D8%A8%D8%AA%D9%85%D8%A8%D8%B1 2 2
Line 255: ar 2_%D9%81%D8%A8%D8%B1%D8%A7%D9%8A%D8%B1 1 1
Uh oh.
java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(BufferedReader.java:574)
at java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1811)
at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
at cl.gdiazc.pagecounts.Main.main(Main.java:57)
Caused by: 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)
at java.io.BufferedReader$1.hasNext(BufferedReader.java:571)
... 3 more
</code></pre>

<p>The input file can be found at: <a href="https://www.dropbox.com/s/ne1140qiapdwvcs/sampleInput.txt?dl=0" rel="nofollow" title="here">https://www.dropbox.com/s/ne1140qiapdwvcs/sampleInput.txt?dl=0</a>. Might anyone have suggestions as to what the difference might be?</p>

<p>(Note: for some context, the original files I'm trying to read are at <a href="https://dumps.wikimedia.org/other/pagecounts-raw/2008/2008-01/" rel="nofollow">https://dumps.wikimedia.org/other/pagecounts-raw/2008/2008-01/</a>)</p>