IO_File_字符集_乱码

版权声明: https://blog.csdn.net/weixin_40072979/article/details/82996956

常用的字符集:UTF-8, GBK, ISO-8859-1,等 

从字符到集字节是编码(电脑是二进制),从字节到字符是解码:

乱码的原因:

     1:可能字节数不够,导致解码乱码。

     2:编码和解码的字符集不统一。

package com.jianshun;

import java.io.UnsupportedEncodingException;

public class ContentEncode {
	public static void main(String[] args) throws UnsupportedEncodingException {
		String msg = "好好学习天天向上";
		//编码:字节数组
		byte[] datas = msg.getBytes();//默认使用工程的字符集
		
		//解码:字符串 
		//msg = new String(datas,"GBK");
		msg = new String(datas,0,datas.length,"GBK");//项目编码为GBK
		System.out.println(msg);//好好学习天天向上
		
		//乱码:
		//1):字节数不够
		msg = new String(datas,0,datas.length-1,"GBK");
		System.out.println(msg);//好好学习天天向?
		
		//2):字符集不统一
		msg = new String(datas,0,datas.length,"UTF-8");
		System.out.println(msg);//?ú???????????
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40072979/article/details/82996956
今日推荐