java獲取一個文本文件的編碼(格式)信息

前言:

文本文件是我們在windows平臺下常用的一種文件格式,

這種格式會隨著操作系統的語言不同,而出現其默認的編碼不同

那麼如何使用程序獲取“文本文件”的編碼方式呢?

文件編碼的格式決定瞭文件可存儲的字符類型,所以得到文件的類型至關重要

下文筆者講述獲取一個文本文件的格式信息的方法分享,如下所示:

現思路:

  • 通過獲取文件流的前3個字節
  • 判斷其值的方式,即可獲取文本文件的編碼方式

例:

package com.java265.other;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class Test {
    /*
     * java265.com 獲取文本文件的編碼方式
     *  
     **/
    public static void main(String[] args)   {
      File file = new File("E://person/java265.com/java.txt");
      System.out.println(GetEncoding(file));
      }
    public static String GetEncoding(File file)
    {
        String charset = "GBK";
        byte[] first3Bytes = new byte[3];
        try {
            boolean checked = false; 
            InputStream is = new FileInputStream(file);
            int read = is.read(first3Bytes, 0, 3);
 
            if (read == -1)
                return charset;
            if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
                charset = "UTF-16LE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xFE
                    && first3Bytes[1] == (byte) 0xFF) {
                charset = "UTF-16BE";
                checked = true;
            } else if (first3Bytes[0] == (byte) 0xEF
                    && first3Bytes[1] == (byte) 0xBB
                    && first3Bytes[2] == (byte) 0xBF) {
                charset = "UTF-8";
                checked = true;
            }else if (first3Bytes[0] == (byte) 0xA
                    && first3Bytes[1] == (byte) 0x5B
                    && first3Bytes[2] == (byte) 0x30) {
                charset = "UTF-8";
                checked = true;
            }else if (first3Bytes[0] == (byte) 0xD
                    && first3Bytes[1] == (byte) 0xA
                    && first3Bytes[2] == (byte) 0x5B) {
                charset = "GBK";
                checked = true;
            }else if (first3Bytes[0] == (byte) 0x5B
                    && first3Bytes[1] == (byte) 0x54
                    && first3Bytes[2] == (byte) 0x49) {
                charset = "windows-1251";
                checked = true;
            }
            //bis.reset();
            InputStream istmp = new FileInputStream(file);
            if (!checked) {
                int loc = 0;
                while ((read = istmp.read()) != -1) {
                    loc++;
                    if (read >= 0xF0)
                        break;
                    if (0x80 <= read && read <= 0xBF)
                        break;
                    if (0xC0 <= read && read <= 0xDF) {
                        read = istmp.read();
                        if (0x80 <= read && read <= 0xBF)
                            continue;
                        else
                            break;
                    } else if (0xE0 <= read && read <= 0xEF) {
                        read = istmp.read();
                        if (0x80 <= read && read <= 0xBF) {
                            read = istmp.read();
                            if (0x80 <= read && read <= 0xBF) {
                                charset = "UTF-8";
                                break;
                            } else
                                break;
                        } else
                            break;
                    }
                }
            }
            is.close();
            istmp.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return charset; 
    }
}

到此這篇關於java獲取一個文本文件的編碼(格式)信息的文章就介紹到這瞭,更多相關java獲取文本編碼內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: