文件的编码

来源:互联网 发布:四川网络电视剧节目表 编辑:程序博客网 时间:2024/06/04 18:46
package com.sh.test;import java.io.UnsupportedEncodingException;import java.util.Iterator;public class IoDemo {public static void main(String[]args) throws UnsupportedEncodingException{String s="我是ABC";//把字符串拆分成字节,转化成字节序列用的是项目默认编码byte[] b=s.getBytes();for(byte by : b){System.out.print(Integer.toHexString(by & 0xff)+" ");//把字节转换成int以16进制的方式显示,相当于在后八位前填24个0变成32位}System.out.println("\nutf编码");//utf-8编码:中文占三个字节英文占一个字节byte[] utf=s.getBytes("utf-8");for (byte utfs : utf) {System.out.print(Integer.toHexString(utfs & 0xff)+" ");}System.out.println("\n默认gbk编码");//gbk编码:中文占用两个字节,英文一个byte[] gbk=s.getBytes("gbk");for (byte gbks : gbk) {System.out.print(Integer.toHexString(gbks & 0xff)+" ");}/*当你的字节序列是某种编码时,这个时候想把字节序列变成字符串,也需要用这种编码方式,否则会出现乱码*/String lm=new String(utf);System.out.println(lm);String gb=new String(utf,"utf-8");System.out.println(gb);/** * 文本文件就是字节序列 * 可以是任意编码的字节序列 * 如果我们在中文机器上直接创建文本文件,那么它只认识ANSI编码 */}}

0 0