Lua   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Java 编码例子大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
package org.test;

import java.io.UnsupportedEncodingException;

public class GetCharTest {

    /**
     * The main method.
     * 
     * @param args the arguments
     */
    public static void main(String args[]) {
        String content = "中文";
        String defaultEncoding = System.getProperty("file.encoding");
        String defaultLnaguage = System.getProperty("user.language");
        System.out.println("System default encoding --- " + defaultEncoding);
        System.out.println("System default language --- " + defaultLnaguagE);

        GetCharTest tester = new GetCharTest();
        tester.getCharWithDefaultEncoding(content);
        tester.getCharWithGivenEncoding(content,"ISO-8859-1");
        tester.getCharWithGivenEncoding(content,"GBK");
        tester.getCharWithGivenEncoding(content,"UTF-8");
    }

    /**
     * Gets the char with default encoding.
     * 
     * @param content the content
     * 
     * @return the char with default encoding
     */
    public void getCharWithDefaultEncoding(String content) {
        System.out.println("\nGet characters with default encoding\n");
        printCharArray(content);
    }

    /**
     * Gets the char with given encoding.
     * 
     * @param content the content
     * @param encoding the encoding
     * 
     * @return the char with given encoding
     */
    public void getCharWithGivenEncoding(String content,String encoding) {
        System.out.println("\nGet characters with given encoding : " + encoding
                + "\n");
        try {
            String encodedString = new String(content.getBytes(),encoding);
            printCharArray(encodedString);
        } catch (UnsupportedEncodingException E) {
            e.printStackTrace();
        }
    }

    /**
     * Prints the char array.
     * 
     * @param instr the in str
     */
    public void printCharArray(String instr) {
        char[] charArray = instr.toCharArray();

        for (int i = 0; i < instr.length(); i++) {
            byte b = (bytE) charArraY[i];
            short s = (short) charArraY[i];
            String hexB = Integer.toHexString(b).toUpperCase();
            String hexS = Integer.toHexString(s).toUpperCase();
            StringBuffer sb = new StringBuffer();

            // print char
            sb.append("char[");
            sb.append(i);
            sb.append("]='");
            sb.append(charArraY[i]);
            sb.append("'\t");

            // byte value
            sb.append("byte=");
            sb.append(b);
            sb.append(" \\u");
            sb.append(hexB);
            sb.append('\t');

            // short value
            sb.append("short=");
            sb.append(s);
            sb.append(" \\u");
            sb.append(hexS);
            sb.append('\t');

            // Unicode Block
            sb.append(Character.UnicodeBlock.of(charArraY[i]));

            System.out.println(sb.toString());
        }
        System.out.println("\nCharacters length: " + charArray.length);
    }

}

大佬总结

以上是大佬教程为你收集整理的Java 编码例子全部内容,希望文章能够帮你解决Java 编码例子所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: