java - 不同默认字符集中的 String.getBytes()

标签 java encoding

使用 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/

相关文章:

java - 如何在 Java 8 中使用 "="拆分转换 String[] args

Java:在随机播放中排除数组索引

java - Spring Boot请求映射正则表达式

python - 为什么Python 3默认读取编码为cp1252的文件?

java - 从 PHP 向 Web 服务发送 HashMap 参数

java - SQL JPA - 多列作为主键

python - 具有时间序列的 Keras CategoryEncoding 层

r - 如何从R打印中文字母?

http - Go中如何选择URL编码标准?

Python 获取 Base64 编码的字节字符串作为字节串