import java.io.UnsupportedEncodingException;
public class CharacterCode {
public static void main(String[] args) {
//这是设置本地字符编码
System.setProperty("file.encoding", "iso8859-1");
//这是第二种方法设置的本地编码
// System.getProperties().put("file.encoding", "iso8859-1");
//这是打印出系统默认一些东西,通过打印出来的东西可以查看本地系统默认的字符编码集是gbk
System.getProperties().list(System.out);
String str = "中国";
for (int i = 0; i < str.length(); i++) {
//这是用java默认的unicode编码字符打印出来
System.out.println(Integer.toHexString((int)(str.charAt(i))));
}
//这是用中文字符的GB2312编码打印出来
byte[] buffer = null;
try {
//这是用iso8859-1编码打印出来,就会出现乱码
buffer = str.getBytes();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (byte b : buffer) {
System.out.println(Integer.toHexString(b));
}
//在显示器的输出流中打印出,利用国标码gb2312
for (byte c : buffer) {
//这个方法不能进行自动刷新缓存区的数据,但是
System.out.write(c);
}
//这个方法会自动调用缓存区的刷新
// System.out.write(buffer, 0, buffer.length);
//PrintStream,System.out.println(),遇到println()这个的时候能够自动刷新缓存区
System.out.println();
//其实下面这句代码,首先"中国"这个字符窜打碎成gb2312,及调用了getBytes("gb2312"),然后调用write方法从屏幕写出来
System.out.println("中国");
}
}