JAVA_字符集

package com.kk.review;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Properties;



public class CharSetTest {
public static void main(String[] args)throws IOException{
getAllSupportedCharSet();
getJVMProperties();
charSetConvertion();
}

/**
* 获取本地jvm支持的编码
*
*/
static void getAllSupportedCharSet() {
Map<String,Charset> sets=Charset.availableCharsets();
for(String str:sets.keySet()){
System.out.println(str);
}
}

/**
* 获取本地jvm的属性,其中包括文件系统的默认格式
*
*/
static void getJVMProperties(){
Properties jvmProperties=System.getProperties();
jvmProperties.list(System.out);
}

/**
* 控制台打印乱码现象
*
@throws UnsupportedEncodingException
*/
static void charSetConvertion()throws UnsupportedEncodingException{
String str=new String("哈哈".getBytes(),"iso-8859-1");
System.out.println(str);//打印乱码,因为console是与本地文件系统格式一样的,我的是GBK
str=new String(str.getBytes("iso-8859-1"),"GBK"); //将iso-8859-1编码转成GBK编码,这样打印出来的就不是乱码了
System.out.println(str);
}
}


编辑器加载中...

原文地址:https://www.cnblogs.com/BigIdiot/p/2282319.html