I have a project where everything is in UTF-8. I was using the Properties.load(Reader) method to read properties files in this encoding. But now, I need to make the project compatible with Java 1.5, and the mentioned method doesn’t exist in Java 1.5. There is only a load method that takes an InputStream as a parameter, which is assumed to be in ISO-8859-1.
Is there any simple way to make my project 1.5-compatible without having to change all the .properties files to ISO-8859-1? I don’t really want to have a mix of encodings in my project (encodings are already a time sink one at a time, let alone when you mix them) or change all my project to ISO-8859-1.
With “a simple way” I mean “without creating a custom Properties class from scratch”.
One strategy that might work for this situation is as follows:
Readerinto aByteArrayOutputStream.Once that is completed, callSee below.toByteArray()byte[]construct aByteArrayInputStreamByteArrayInputStreaminProperties.load(InputStream)As pointed out, the above failed to actually convert the character set from UTF-8 to ISO-8859-1. To fix that, a tweak.
After the BAOS has been filled, instead of calling
toByteArray()..toString("ISO-8859-1")to get an ISO-8859-1 encodedString. Then look to..String.getBytes()to get thebyte[]