26

I have this (stripped the HTML tags for the code example) function that builds a HTML table out of a CSV, but I get a runtime error everytime I try to run it and I don't know why. Google says that maybe something with the encoding is wrong but I have no idea how to change that.

My CSV is encoded in ANSI and contains characters like ä, Ä, Ü, Ö but I have no control over the encoding or if it will change in the future.

The error occurs here:

Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1
at java.io.BufferedReader$1.hasNext(Unknown Source)
at java.util.Iterator.forEachRemaining(Unknown Source)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source)
at testgui.Csv2Html.start(Csv2Html.java:121)

Line 121 is

lines.forEach(line -> {

Sourcecode:

protected void start() throws Exception {

    Path path = Paths.get(inputFile);

    FileOutputStream fos = new FileOutputStream(outputFile, true);
    PrintStream ps = new PrintStream(fos);      

    boolean withTableHeader = (inputFile.length() != 0);
    try  {
        Stream<String> lines = Files.lines(path);
        lines.forEach(line -> {
            try {
                String[] columns = line.split(";");
                for (int i=0; i<columns.length; i++) {
                    columns[i] = escapeHTMLChars(columns[i]);
                }       
                if (withTableHeader == true && firstLine == true) {
                    tableHeader(ps, columns);
                    firstLine = false;
                } else {
                    tableRow(ps, columns);
                }


            } catch (Exception e) {
                e.printStackTrace();
            } finally {

            }
        });

    } finally {
        ps.close();
    }

}
Vega
  • 2,661
  • 5
  • 24
  • 49

1 Answers1

41

You can try to utilize the correct encoding by using the Files.lines(Path path, Charset charset) form of the lines method (javadocs).

Here's a list of supported encodings (for the Oracle JVM anyhow). This post indicates that "Cp1252" is Windows ANSI.

Community
  • 1
  • 1
blazetopher
  • 1,050
  • 9
  • 13
  • 4
    Thanks, that fixed it :) - Stream lines = Files.lines(Paths.get(inputFile), Charset.forName("Cp1252")); Do you also know where I have to close my Stream? Eclipse warns me about a ressource leak because I don't call lines.close() but I don't know where to put it, tried the finally {} blocks but that gives me an exception because it seems I closed the Stream too early. – Vega Jun 03 '15 at 01:55
  • 4
    You can use the try-with-resources to close the stream automatically after it's finished being used: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html try (Stream lines = Files.lines(Paths.get(inputFile), Charset.forName("Cp1252")) { ... your parsing code here ... } – blazetopher Jun 03 '15 at 01:55
  • 1
    thanks for the response, on how to fix I had this problem too when reading file using streams, when I read file using BufferedReader(FileReader) implementation error did not show so it was driving me mad, thanks for showing Stream api's handle different encoding for reading csv files. – Gabriel Hernandez Jul 18 '20 at 17:05