public byte[] getBytes(Charset charset)

Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. 

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array. The CharsetEncoder class should be used when more control over the encoding process is required.

使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。

当此字符串不能使用给定的字符集编码时,此方法的行为没有指定。如果需要对编码过程进行更多控制,则应该使用 CharsetEncoder 类。

在Java中,String.getBytes(String decode)方法会根据指定的decode编码返回某字符串在该编码下的byte数组表示,如

byte[] b_gbk = "中".getBytes("GBK");
byte[] b_utf8 = "中".getBytes("UTF-8");
byte[] b_iso88591 = "中".getBytes("ISO8859-1");

将分别返回“中”这个汉字在GBK、UTF-8和ISO8859-1编码下的byte数组表示。

此时b_gbk的长度为2,b_utf8的长度为3,b_iso88591的长度为1。

public String(byte[] bytes, Charset charset)

Constructs a new String by decoding the specified array of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。新 String 的长度是字符集的函数,因此可能不等于 byte 数组的长度。 

此方法总是使用此字符集的默认替代字符串替代错误输入和不可映射字符序列。如果需要对解码过程进行更多控制,则应该使用 CharsetDecoder 类。

而与getBytes相对的,可以通过new String(byte[], decode)的方式来还原这个“中”字。

这个new String(byte[], decode)实际是使用decode指定的编码来将byte[]解析成UNICODE字符串。(按照什么存就得按照什么解)

String s_gbk = new String(b_gbk,"GBK");
String s_utf8 = new String(b_utf8,"UTF-8");
String s_iso88591 = new String(b_iso88591,"ISO8859-1");

通过打印s_gbk、s_utf8和s_iso88591,会发现,s_gbk和s_utf8都是“中”,而只有s_iso88591是?,为什么使用ISO8859-1编码再组合之后,无法还原“中”字呢,其实原因很简单,因为ISO8859-1编码的编码表中,根本就没有包含汉字字符,当然也就无法通过"中".getBytes("ISO8859-1");来得到正确的“中”字在ISO8859-1中的编码值了,所以再通过new String()来还原就无从谈起了。

因此,通过String.getBytes(String decode)方法来得到byte[]时,一定要确定decode的编码表中确实存在String表示的码值,这样得到的byte[]数组才能正确被还原。

有时候,为了让中文字符适应某些特殊要求(如http header头要求其内容必须为iso8859-1编码),可能会通过将中文字符按照字节方式来编码的情况,如

String s_iso88591 = new String("中".getBytes("UTF-8"),"ISO8859-1"),

这样得到的s_iso8859-1字符串实际是三个在 ISO8859-1中的字符,在将这些字符传递到目的地后,

目的地程序再通过相反的方式String s_utf8 = new String(s_iso88591.getBytes("ISO8859-1"),"UTF-8")来得到正确的中文汉字“中”。这样就既保证了遵守协 议规定、也支持中文。

String str = "中国";
String iso88591 = new String(str.getBytes("UTF-8"), "ISO-8859-1");
str = new String(iso88591.getBytes("ISO-8859-1"), "UTF-8");
System.out.println(str);
import java.io.UnsupportedEncodingException;

/**
* Created by N3verL4nd on 2017/1/2.
*/ public class HelloWorld
{
public static void main(String[] args) throws UnsupportedEncodingException {
String str=new String("我爱天安门");
byte by_gbk[]=str.getBytes("GBK");
String str_gbk=new String(by_gbk,"GBK");
System.out.println("str_gbk:"+str_gbk);
String str_utf8=new String(by_gbk,"UTF-8");
System.out.println("str_utf8:"+str_utf8);
System.out.println("----------------------");
byte by_utf8[]=str.getBytes("UTF-8");
String str2_gbk=new String(by_utf8,"GBK");
System.out.println("str2_gbk:"+str2_gbk);
String str2_utf8=new String(by_utf8,"UTF-8");
System.out.println("str2_utf8:"+str2_utf8);
}
}

http://www.cnblogs.com/caowei/p/2013-12-11_request-response.html

http://blog.csdn.net/tianjf0514/article/details/7854624

关于java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)的更多相关文章

  1. 用java String类的getBytes(String charsetName)和String(byte[] bytes, String charsetName)解决乱码问题

    Java中String的数据是如何存储的,查看源代码就可以知道,String的数据是存储在char[] value这样一个成员变量中的,char类型的大小在java中是2个字节 我们还知道,现在普遍使 ...

  2. java 中String类的常用方法总结,带你玩转String类。

    String类: String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.String类对象创建后不能修改,StringBuffer & St ...

  3. String(byte[] bytes, String charsetName)

    String str = new String("时之沙"); byte bytes[] = str.getBytes("GBK"); byte byte2[] ...

  4. String 类的实现(3)引用计数实现String类

    我们知道在C++中动态开辟空间时是用字符new和delete的.其中使用new test[N]方式开辟空间时实际上是开辟了(N*sizeof(test)+4)字节的空间.如图示其中保存N的值主要用于析 ...

  5. 深入理解Java常用类----String

         Java中字符串的操作可谓是最常见的操作了,String这个类它封装了有关字符串操作的大部分方法,从构建一个字符串对象到对字符串的各种操作都封装在该类中,本篇我们通过阅读String类的源码 ...

  6. java.lang.String 类源码解读

    String类定义实现了java.io.Serializable, Comparable<String>, CharSequence 三个接口:并且为final修饰. public fin ...

  7. Java之String类常用API

    目录 Java之String类常用API char chatAt(int index) int length() char[] toCharArray() String(char value[]) S ...

  8. Java常用类(一)String类详解

    前言 在我们开发中经常会用到很多的常用的工具类,这里做一个总结.他们有很多的方法都是我们经常要用到的.所以我们一定要把它好好的掌握起来! 一.String简介 1.1.String(字符串常量)概述 ...

  9. java常用类:1。包装类(以Integer类为例)2.String类 3.StringBuffer

     包装类 把八大基本数据类型封装到一个类中,并提供属性和方法,更方便的操作基本数据类型. 包装类的出现并不是用于取代基本数据类型,也取代不了. 包装类位于java.lang包中. Number 类 N ...

随机推荐

  1. HashMap 源码赏析 JDK8

    一.简介 HashMap源码看过无数遍了,但是总是忘,好记性不如烂笔头. 本文HashMap源码基于JDK8. 文章将全面介绍HashMap的源码及HashMap存在的诸多问题. 开局一张图,先来看看 ...

  2. 【转】经典的SQL语句面试题

    Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname,T#) 课程表SC(S#,C#,score) 成绩表Teacher(T#,Tname) 教师表 问题: 1 ...

  3. MariaDB的备份与主从、高可用实践

    1.编写脚本,支持让用户自主选择,使用mysqldump还是xtraback全量备份. [root@test-centos7-node1 scripts]# cat chose_backup_mysq ...

  4. linux入门系列7--管道符、重定向、环境变量

    前面文章我们学习了linux基础命令,如果将不同命令组合使用则可以成倍提高工作效率.本文将学习重定向.管道符.通配符.转义符.以及重要的环境变量相关知识,为后面的shell编程打下基础. 一.IO重定 ...

  5. SpringBoot + Apache Shiro权限管理

    之前配置过Spring + SpringMVC + JPA + Shiro后台权限管理 + VUE前台登录页面的框架,手动配置各种.xml,比较繁琐,前几天写了个SpringBootShiro的Dem ...

  6. SpringCloud之Eureka(注册中心集群篇)(三)

    一:集群环境搭建 第一步:我们新建两个注册中心工程一个叫eureka_register_service_master.另外一个叫eureka_register_service_backup eurek ...

  7. 如何优雅的用策略模式,取代臃肿的 if-else 嵌套,看这篇就够了

    经常听同事抱怨,订单来源又加了一种,代码又要加一层if-else判断,光判断订单来源的if-else就好几百行代码,代码我都不想看了,相信很多同行都有过这样的感受! Java的二十几种设计模式背的滚瓜 ...

  8. Android Studio 图形化设计 UI 界面

    我们开发 Android 程序必定是从 UI 开始的 ,使用最新版的 Android Studio 可以在图形化界面下设计软件 UI, Android Studio 默认的布局是 Constraint ...

  9. C# 根据年月日计算周次

    //day:要判断的日期,WeekStart:1 周一为一周的开始, 2 周日为一周的开始 public static int WeekOfMonth(DateTime day, int WeekSt ...

  10. 用户输入- Unity3D游戏开发培训

    用户输入- Unity3D游戏开发培训   作者:Jesai 时间:2018-02-12 14:28:45 用户输入Input 鼠标按键: -方法:GetMouseButton(); -方法:GetM ...