javacharacter-encodingdeprecatedfileutilsioutils

Replacing deprecated Java method


I have multiple calls that use deprecated methods, such as

org.apache.commons.io.FileUtils.readFileToString(File file)

org.apache.commons.io.IOUtils.toString(InputStream input)
org.apache.commons.io.IOUtils.toInputStream(String input)

Now, I do know that these methods are deprecated and with which methods to replace them, for example

readFileToString(final File file, final Charset encoding)

The difference being that I have to specify an encoding.

Now, because my primary goal is to preserve behaviour while removing the old methods, even if it means preserving bugs/oddities, I want to call the new method such that it works just like the old one. Reading the Javadoc for the new method reveals

* @param encoding the encoding to use, {@code null} means platform default

My assumption is now that if I pass null as encoding parameter to the new method, it will behave just like the old one. That would imply that the old method always used the platform default. But is there a way to check that?


Solution

  • As expected it used the default encoding, readFileToString:

    Reads the contents of a file into a String using the default encoding for the VM. The file is always closed.

    And IOUtils.toString:

    Gets the contents of a byte[] as a String using the default character encoding of the platform.