javajava-11charset

MalformedInputException when reading file using Apache FileUtils


FileUtils.readLines(attribFile, StandardCharsets.UTF_8) This line is giving the error: Method threw java.nio.charset.MalformedInputException
What could be the reason for this?
Is it because the File could contain characters that are not supported by UTF_8?
But when I check the JSON response of the HTTP GET request from which the file is built I don't see any special characters. I just see , - . \n :
Any ideas on how do I debug this further?

Some Things I tried:
There is no error when I use FileUtils.readLines(attribFile). This seems to be working properly. Although the function is deprecated.

Thing that worked:
To generate the input file I was using PrintWriter pw = new PrintWriter(attribFile) I changed this to PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(attribFile),"UTF-8") and it worked fine.

But this really gave me a headache. I am used to coding like PrintWriter pw = new PrintWriter(attribFile) without specifying the charset. This seems to be a bug/code quality issue on the methods that it allows to create an object without specifying the charset. Probably that is why FileUtils.readLines is deprecated without specifying the charset.
Looking for thoughts on the above enter image description here


Solution

  • You wrote

    This seems to be a bug/code quality issue on the methods that it allows to create an object without specifying the charset.

    and the simple answer is that you are right, though this is a long standing, well known issue. So any code audit tool should be able to warn you about this.

    Further, the issue has been addressed in Java 18. Starting with this version, all methods without a specified encoding will consistently use UTF-8 by default.

    As mentioned in the comments, you can use the methods in java.nio.file.Files to read and write text without the need for a 3rd party library and the methods in this class use UTF-8 when no character encoding has been specified, in all Java versions.