Java獲取字符串編碼格式實(shí)現(xiàn)思路
Java——獲取字符串編碼格式
判斷一個字符串的編碼格式:
public static String getEncoding(String str) { String encode = "GB2312"; try { if (isEncoding(str, encode)) { // 判斷是不是GB2312 return encode; } } catch (Exception exception) { } encode = "ISO-8859-1"; try { if (isEncoding(str, encode)) { // 判斷是不是ISO-8859-1 return encode; } } catch (Exception exception1) { } encode = "UTF-8"; try { if (isEncoding(str, encode)) { // 判斷是不是UTF-8 return encode; } } catch (Exception exception2) { } encode = "GBK"; try { if (isEncoding(str, encode)) { // 判斷是不是GBK return encode; } } catch (Exception exception3) { } return ""; // 如果都不是,說明輸入的內(nèi)容不屬于常見的編碼格式。 } public static boolean isEncoding(String str, String encode) { try { if (str.equals(new String(str.getBytes(), encode))) { return true; } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return false; }
Java獲取一個文本文件的編碼格式
文本文件是我們在windows平臺下常用的一種文件格式,這種格式會隨著操作系統(tǒng)的語言不同,而出現(xiàn)其默認(rèn)的編碼不同
那么如何使用程序獲取“文本文件”的編碼方式呢?
文件編碼的格式?jīng)Q定了文件可存儲的字符類型,所以得到文件的類型至關(guān)重要
下文筆者講述獲取一個文本文件的格式信息的方法分享,如下所示:
實(shí)現(xiàn)思路:
通過獲取文件流的前3個字節(jié)
判斷其值的方式,即可獲取文本文件的編碼方式
例:
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; } }
到此這篇關(guān)于Java獲取一個文本文件的編碼格式的實(shí)現(xiàn)思路的文章就介紹到這了,更多相關(guān)java文件編碼格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談Spring Boot 2.0遷移指南主要注意點(diǎn)
Spring官方的Spring Boot 2變動指南,主要是幫助您將應(yīng)用程序遷移到Spring Boot 2.0,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10SpringBoot統(tǒng)一響應(yīng)格式及統(tǒng)一異常處理
在我們開發(fā)SpringBoot后端服務(wù)時,一般需要給前端統(tǒng)一響應(yīng)格式,本文主要介紹了SpringBoot統(tǒng)一響應(yīng)格式及統(tǒng)一異常處理2023-05-05Springboot如何使用@Async實(shí)現(xiàn)異步任務(wù)
這篇文章主要介紹了Springboot如何使用@Async實(shí)現(xiàn)異步任務(wù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09StreamAPI多次消費(fèi)一個stream代碼實(shí)例
這篇文章主要介紹了StreamAPI多次消費(fèi)一個stream代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04