Java 编码例子

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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);
    }

}