小說軟體開發,java獲取文字檔案的編碼格式
小說軟體開發,java獲取文字檔案的編碼格式實現的相關程式碼
/** * 獲取檔案的編碼格式 * @param file * @return */ public String GetCharset(File file) { String charset = "GBK"; byte[] first3Bytes = new byte[3]; try { boolean checked = false; //BufferedInputStream bis = new BufferedInputStream( //new FileInputStream(file)); //bis.mark(0); //int read = bis.read(first3Bytes, 0, 3); InputStream is = new FileInputStream(file); int read = is.read(first3Bytes, 0, 3); if (read == -1) return charset; //Log.i(TAG,"[GetCharset]first3Bytes[0]:"+first3Bytes[0]+", first3Bytes[1]:"+first3Bytes[1]+",first3Bytes[2]:"+first3Bytes[2]); 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獲取文字檔案的編碼格式實現的相關程式碼,更多內容歡迎關注之後的文章
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2845378/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 文字檔案的編碼格式
- 前端獲取視訊編碼格式前端
- Loadrunner+引數化檔案編碼格式+獲取請求報文發生異常
- 如何修改檔案的編碼格式
- Java讀取文字檔案中文亂碼問題Java
- ofd是什麼格式的檔案 ofd格式檔案用什麼軟體開啟
- 軟體著作權檔案格式
- 電腦如何開啟ofd格式檔案 什麼軟體可以開啟ofd格式檔案
- windows文字檔案格式?Windows
- Android開發:APK的反編譯(獲取程式碼和資原始檔)AndroidAPK編譯
- iOS文字檔案的編碼檢測iOS
- LINUX下 Python獲取xlsx格式的檔案內容轉到txt 小文字LinuxPython
- Storyist for Mac(mac小說編輯軟體)Mac
- 直播軟體搭建,java程式碼獲取記憶體資訊Java記憶體
- dwg格式檔案用什麼軟體開啟(線上看dwg格式檔案)
- mpp檔案用什麼軟體開啟 如何開啟mpp格式檔案
- Linux下檢視檔案編碼,檔案編碼格式轉換和檔名編碼Linux
- msn密碼獲取軟體密碼
- dwg格式檔案用什麼軟體開啟 dwg是什麼檔案格式
- dxf是什麼檔案格式 dxf檔案格式用什麼軟體怎麼開啟
- java Properties獲取本地檔案的變數Java變數
- Phylip進化樹的使用,偏重於檔案格式的獲取
- plt是什麼格式檔案 plt檔案要用什麼軟體開啟
- swf是什麼檔案格式 swf檔案要用什麼軟體開啟
- Java中的獲取檔案的物理絕對路徑,和讀取檔案Java
- 讀取txt檔案將文字行組合成特定格式
- Java爬蟲學習——例項:獲取起點中文網站小說並儲存成txt檔案Java爬蟲網站
- PKI 證書檔案編碼格式介紹
- 如何修改geditor的配置檔案 -好像geditor沒有文字格式的配置檔案? 要使用dconf-editor來配置- geditor自己配置編碼格式...
- C#讀取文字檔案和寫文字檔案C#
- java 猜測 檔案編碼Java
- Mac文字編輯軟體Mac
- rtf是什麼格式的檔案 rtf用什麼軟體開啟
- 怎樣從ipa格式檔案獲取資源圖片
- 小說軟體原始碼的快取設計,保證服務的正常執行原始碼快取
- PDF檔案掃描文字識別軟體
- 指令碼:獲取當前的User Trace檔案指令碼
- 【字元編碼】Java編碼格式探祕字元Java