package com.Lucky.io.Code; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Arrays; /* java中编码方法: getByte() 使用默认的方式进行编码 getByte(String charsetName) 使用指定的方式进行编码 java中解码方法: String(byte[] bytes) 使用默认的方式进行解码 String(byte[] bytes,String charsetName) 使用指定的方式进行编码 */ /** * 每天一个小知识: * 出现乱码的原因:编码解码格式不同 * 注意点: * 防止出现乱码条件:①不要使用字节流操作纯文本②编码解码格式要相同 */ public class CharSetDemo { public static void main(String[] args) throws UnsupportedEncodingException { String str="ai你哟"; System.out.println("--1.编码--"); byte[] bytes = str.getBytes(); System.out.println(Arrays.toString(bytes)); //在idea中默认的编码格式就是UTF-8 byte[] gbks = str.getBytes("GBK"); System.out.println(Arrays.toString(gbks)); //在eclipse中默认的编码格式就是GBK byte[] asciis = str.getBytes("ASCII"); System.out.println(Arrays.toString(asciis)); System.out.println("--2.解码--"); String resUTF=new String(bytes); System.out.println(resUTF); String resGBK=new String(gbks); System.out.println(resGBK); //乱码出现 } }
标签:编码,String,解码,charSet,System,println,out From: https://www.cnblogs.com/Lucky-only/p/16970747.html