javaapache-commons-lang3

Apache Commons ToStringBuilder String encoding


I'm using Apache Commons' ToStringBuilder to generate entities toString's block content. But for some entities, there are String fields encoded with "ISO-8859-15", so I'm wondering if the produced toString will use platform's default (UTF-8 on my vm) with values outputed incorrectly.

Thanks for your help


Solution

  • Java strings are always using Unicode for characters.

    Encoding only becomes an issue when reading/writing strings from/to files. Or when converting strings into byte arrays for example.

    In that sense: you do not have to worry about the string builder. You have to worry about "where are is the data initially coming from" respectively "going to".

    Just for completeness: yes, the Java String class has a constructor where you can pass in the desired encoding - but that constructor also takes an array of bytes as first argument. And as said: in such cases you have to worry about encoding.