Is it safe to use String.getBytes() ? What happens when a program runs on different systems with different default charset?
I suppose I can get different content byte[]?
Is it possible to define preferred charset in Java 1.4?
Is it safe to use
String.getBytes()
?
It depends on what you mean by "safe". It will do exactly what you're trying to do.
What will happens when program will run on different systems with different default charset? I suppose I can get different content byte []?
Yes. Often you won't spot any difference if your string only contains ASCII, but even then there can be significant differences - e.g. in UTF-16 each character will take two bytes.
Is it possible to define preferred charset in java 1.4?
Not that I'm aware of. I don't know of a standard system property for this, for example. There may well be one for the specific implementation you're using, of course. It depends on your context. (You could set the file.encoding
system property on the command line, for example. Whether or not that will affect the default encoding depends on the VM. It's not listed in System.getProperties
.)
I would personally always specify the encoding you want to use, using the overloads which take a charset name or a Charset
. On the rare occasions where you actually want to use the system default, just specify that explicitly (e.g. with Charset.defaultCharset
).
See more on this question at Stackoverflow