使用 String.getBytes() 安全吗?当程序在具有不同默认 charset 的不同系统上运行时会发生什么?
我想我可以获得不同的内容 byte[]?
是否可以在 中定义首选字符集Java 1.4?
最佳答案
Is it safe to use String.getBytes() ?
没有。您应该始终使用 the overload which specifies the charset;理想情况下使用 UTF-8 everywhere.如果您使用的是现代版本的 Java,您的代码可以使用 StandardCharsets
美好清洁生活。
What will happens when program will run on different systems with different default charset?
您的代码存在使用错误编码解释字符数据的风险,从而导致字符串损坏/不正确(例如:"î"
、"ÃÂ"
、 "ü"
) 和/或替换字符 (�)。
Is it possible to define preferred charset in java 1.4?
No. The platform-default is, by definition, dictated by the platform, not your app.
关于java - 不同默认字符集中的 String.getBytes(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19098211/