牛骨文教育服务平台(让学习变的简单)
博文笔记

(透彻)java String.getBytes()编码问题

创建时间:2012-03-22 投稿人: 浏览次数:143

转载自:

String.getBytes()的问题

String的getBytes()方法是得到一个字串的字节数组,这是众所周知的。但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组。如果你在使用这个方法时不考虑到这一点,你会发现在一个平台上运行良好的系统,放到另外一台机器后会产生意想不到的问题。比如下面的程序:
class TestCharset { 
public static void main(String[] args) { 
new TestCharset().execute(); 
private void execute() { 
String s = "Hello!你好!"; 
byte[] bytes = s.getBytes();
System.out.println("bytes lenght is:" + bytes.length); 
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。