Getting "java.nio.charset.UnmappableCharacterException: Input length = 1"

I am getting UnmappableCharacterException on the collect() method call (or on the toList() call):

    private static void handleTransaction(Path a_filePath, String a_sTrasactionName, String a_sTransactionFilePath) {

    // read file into stream, try-with-resources
    try (Stream<String> stream = Files.lines(Paths.get(a_filePath.toString()), Charset.defaultCharset())) {

        List<String> list =
            stream.filter(line -> (line.indexOf(a_sTrasactionName) > 0))
            .collect(Collectors.toList());

        list.forEach(line -> {

            System.out.println(line);

            try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(_FILES_PATH + a_sTransactionFilePath),Charset.defaultCharset(), StandardOpenOption.APPEND)) {
                writer.write(line + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    } catch (IOException e1) {

        e1.printStackTrace();
    }

It worked for me once, but never since then.

The files I read are csv files which were created on solaris. I run the jar on Windows 2012 server

Can you advise please?

Thank you.

Jon Skeet
people
quotationmark

The files I read are csv files which were created on solaris. I run the jar on Windows 2012 server

Well that's probably the problem then. You're using the platform-default encoding for both reading and writing the file. If the files were created on Solaris, that may very well have a different platform-default encoding to your Windows box.

If you know the encoding of the file you're reading, specify that.

If you get to control the encoding of the file you're reading and writing, I would strongly recommend using UTF-8 unless you have a really good reason not to.

Only use Charset.default() if you're reading a file which you know uses the platform-default encoding, or if you're writing a file which you definitely want to use the platform-default encoding - and try to avoid the latter.

(Basically, a world where everything is encoded in UTF-8 is a simpler world...)

people

See more on this question at Stackoverflow