Java 怎么确定字符串编码

2019年8月11日20:50:46Java 怎么确定字符串编码已关闭评论 429 1252字阅读4分10秒

1.简介

Java 怎么确定字符串编码?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String determineEncoding(InputStream stream)
            throws IOException {
        stream.mark(20000);
        try {
            int b0 = stream.read();
            int b1 = stream.read();
            int b2 = stream.read();
            int b3 = stream.read();

            if (b0 == 0xFE && b1 == 0xFF)
                return "UTF-16BE"; 
            else if (b0 == 0xFF && b1 == 0xFE)
                return "UTF-16LE"; 
            else if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF)
                return "UTF-8"; 
            else if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F)
                return "UTF-16BE"; 
            else if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00)
                return "UTF-16LE"; 
            else if (b0 == 0x3C && b1 == 0x3F && b2 == 0x78 && b3 == 0x6D) {
                InputStreamReader rdr = new InputStreamReader(stream,
                        "US-ASCII"); 
                String hdr = readFirstLine(rdr);
                return extractEncoding(hdr);
            } else
                return null;
        } finally {
            stream.reset();
        }
    }

    private static String readFirstLine(InputStreamReader rdr)
            throws IOException {
        char[] buf = new char[1];
        StringBuffer bldr = new StringBuffer();
        rdr.read(buf);
        while (buf[0] != '>') {
            bldr.append(buf[0]);
            rdr.read(buf);
        }
        return bldr.toString();
    }

    private static String extractEncoding(String hdr) {
        int i = hdr.indexOf("encoding="); 
        if (i == -1)
            return null;
        hdr = hdr.substring(i + 9);
        char sep = hdr.charAt(0);
        hdr = hdr.substring(1);
        i = hdr.indexOf(sep);
        if (i == -1)
            return null;
        return hdr.substring(0, i);
    }
}

 
继续阅读
编程十万个怎么办