Reliance on default encoding, what should I use and why?

前端 未结 4 1789
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 14:36

FindBugs reports a bug:

Reliance on default encoding Found a call to a method which will perform a byte to String (or String to byte) conversion, a

4条回答
  •  鱼传尺愫
    2020-12-28 14:59

    You should use default encoding whenever you read a file that is outside your application and can be assumed to be in the user's local encoding, for example user written text files. You might want to use the default encoding when writing such files, depending on what the user is going to do with that file later.

    You should not use default encoding for any other file, especially application relevant files.

    If you application for example writes configuration files in text format, you should always specify the encoding. In general UTF-8 is always a good choice, as it is compatible to almost everything. Not doing so might cause surprise crashes by users in other countries.

    This is not only limited to character encoding, but as well to date/time, numeric or other language specific formats. If you for example use default encoding and default date/time strings on a US machine, then try to read that file on a German server, you might be surprised why one half is gibberish and the other half has month/days confused or is off by one hour because of daylight saving time.

提交回复
热议问题