编程语言
首页 > 编程语言> > java NIO———字符集、编码与解码

java NIO———字符集、编码与解码

作者:互联网

1 获取字符集

//1.获取所有字符集
    @Test
    public void test01(){
        Map<String,Charset> charsetMap = Charset.availableCharsets();
        Set<Map.Entry<String,Charset>> set = charsetMap.entrySet();
        for(Map.Entry<String,Charset> entry:set){
            System.out.println(entry.getKey()+"="+entry.getValue());
        }
    }

在这里插入图片描述
2、编码与解码测试

    //2.编码与解码
    @Test
    public void test02(){
        //选择编码
        Charset charset = Charset.forName("GBK");
        //获取编码器与解码器解码器
        CharsetEncoder encoder = charset.newEncoder();
        CharsetDecoder decoder = charset.newDecoder();
        CharBuffer charBuffer = CharBuffer.allocate(512);
        charBuffer.put("人生如梦,一樽还酹江月");
        charBuffer.flip();

        try {
            //编码
            System.out.println("after encoding by GBK:");
            ByteBuffer byteBuffer = encoder.encode(charBuffer);
            for(int i=0;i<byteBuffer.limit();i++){
                System.out.print(byteBuffer.get());
            }
            System.out.println("\nafter decoding by utf-8:");
            byteBuffer.flip();
            Charset charset1 = Charset.forName("UTF-8");
            CharBuffer charBuffer1 = charset1.decode(byteBuffer);
            System.out.println(charBuffer1.toString());
//            encode("编码与解码学习","UTF-8");

//            解码
            byteBuffer.flip();
            System.out.println("after decoding by GBK:");
            CharBuffer charBuffer2 = decoder.decode(byteBuffer);
            System.out.println(charBuffer2.toString());
//            decode(byteBuffer,"ISO-8859-1");

        } catch (CharacterCodingException e) {
            e.printStackTrace();
        }


    }

3.自己封装的解码编码方法

/**
     * 自己封装的解码方法
     * @param byteBuffer 要解码的ByteBuffer
     * @param charsetname 字符集
     * @return
     */
    public String decode(@NotNull ByteBuffer byteBuffer, String charsetname){
        System.out.println("after decoding by "+charsetname);
        if(byteBuffer==null) return null;
        if(charsetname==null) charsetname="UTF-8";
        Charset charset =Charset.forName(charsetname);
        byteBuffer.flip();
        CharBuffer charBuffer = charset.decode(byteBuffer);
        String  result = charBuffer.toString();
        System.out.println(charsetname+":"+result);
        return result;
    }

    /**
     * 自己封装的编码方法
     * @param str 要编码的字符串
     * @param charsetname 编码字符集
     * @return
     */
    public ByteBuffer encode(@NotNull String str, String charsetname){
        System.out.println("after encoding by "+charsetname);
        if(str==null||"".equals(str)) return null;
        if(charsetname==null) charsetname="UTF-8";
        Charset charset = Charset.forName(charsetname);
        ByteBuffer byteBuffer = charset.encode(str);
        for(int i=0;i<byteBuffer.limit();i++){
            System.out.println(byteBuffer.get(i));
        }
        return byteBuffer;
    }

在这里插入图片描述

盈昃 发布了9 篇原创文章 · 获赞 2 · 访问量 219 私信 关注

标签:Charset,java,NIO,charBuffer,charset,字符集,byteBuffer,null,charsetname
来源: https://blog.csdn.net/qq_43615903/article/details/104161011