I'm using the FindBug program from Maryland University and it gives me this error.
I've tested my code on numerous platforms and it works, so why is this code bad-practice, and what can I do to improve it?
I'm using the FindBug program from Maryland University and it gives me this error.
I've tested my code on numerous platforms and it works, so why is this code bad-practice, and what can I do to improve it?
you need to specify the charset
you can use anOutputStreamWriter
See the FileWriter documentation: "The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream."
It can be considered bad practice to depend on default character encoding.
Use
FileOutputStream
, instead ofFileWriter
. Which can be wrapped using theOutputStreamWriter
, which allows you to pass an encoding in the constructor.Or else, as said by Jeff, the data won't load correctly.
Example
It's telling you the encoding (how the string is turned into bytes) isn't specified.
If you write a text file in Turkey, and load it up in Uzbekistan then you might get different results. Instead (for example) you could specify the encoding directly by converting the string to bytes yourself using a specified encoding (see String.getBytes for an example).