Java编码测试

来源:互联网 发布:鼓浪屿酒店推荐 知乎 编辑:程序博客网 时间:2024/06/07 10:16

http://blog.csdn.net/xxb2008

import java.nio.charset.Charset;public class Helloworld{    //指定defaultCharset默认编码:-Dfile.encoding=GBK    //如果没有指定,系统会根据时区、操作系统的相关信息来获取。    //javac -encoding utf-8 Helloworld.java,如果文件为GBK编码,会报错。    //java  -Dfile.encoding=utf-8 Helloworld    //javap -v Helloworld.class    public static void main(String[] args){      try{        User u = new User();        u.name="我";        System.out.println(u.name);        System.out.println(Charset .defaultCharset()); //UTF-8        System.out.println("默认长度-->" + u.name.getBytes().length);        System.out.println("utf8长度-3->" + u.name.getBytes("utf-8").length);        System.out.println("gbk长度-2->" + u.name.getBytes("GBK").length);           }catch(Exception e){}    } }




0 0
原创粉丝点击