黑马JAVA P157 IO前置内容:字符集

黑马JAVA P157 IO前置内容:字符集,第1张

黑马JAVA P157 IO前置内容:字符集

 

 


 

 

package com.itheima.d3_charaset;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;


public class Test {
    public static void main(String[] args) throws Exception {
        //1.编码:把文字转换成字节(使用指定的编码)
        String name = "abc我爱你中国";
//        byte [] bytes = name.getBytes();//以当前代码默认字符集进行编码 (UTF-8)
        byte [] bytes = name.getBytes("GBK"); //指定编码
        System.out.println(bytes.length);
        System.out.println(Arrays.toString(bytes));

        //2.解码:把字节转换成对应的中文形式(编码前 和 编码后的字符集必须一致,否则乱码)
//        String rs = new String(bytes); //默认的UTF-8
                String rs = new String(bytes,"GBK"); //指定GBK编码
        System.out.println(rs);

    }
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713146.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存