java识别文件或字符串的编码格式
1, 用juniversalchardet:
http://code.google.com/p/juniversalchardet/
官方示例:
import org.mozilla.universalchardet.UniversalDetector; public class TestDetector { public static void main(String[] args) { byte[] buf = new byte[4096]; java.io.FileInputStream fis = new java.io.FileInputStream("test.txt"); // (1) UniversalDetector detector = new UniversalDetector(null); // (2) int nread; while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { detector.handleData(buf, 0, nread); } // (3) detector.dataEnd(); // (4) String encoding = detector.getDetectedCharset(); if (encoding != null) { System.out.println("Detected encoding = " + encoding); } else { System.out.println("No encoding detected."); } // (5) detector.reset(); } }
他人示例代码:
public static String guessEncoding(byte[] bytes) { String DEFAULT_ENCODING = "UTF-8"; org.mozilla.universalchardet.UniversalDetector detector = new org.mozilla.universalchardet.UniversalDetector(null); detector.handleData(bytes, 0, bytes.length); detector.dataEnd(); String encoding = detector.getDetectedCharset(); detector.reset(); if (encoding == null) { encoding = DEFAULT_ENCODING; } return encoding; }
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
支付宝扫一扫捐赠
支付宝扫一扫捐赠
![支付宝扫一扫捐赠](https://images.cnblogs.com/cnblogs_com/welhzh/2159800/o_220512091701_hzh_alipay.jpg)
微信公众号: 共鸣圈
欢迎讨论,邮件: 924948$qq.com 请把$改成@
QQ群:263132197
QQ: 924948