Java 取得文件编码格式<转>
Java 获得文件编码格式转Java判断文件编码集/**?????????????????????????????????????????????????????
Java 获得文件编码格式<转>
Java判断文件编码集/**??????????????????????????????????????????????????????????????????????????????
* 获得远程URL文件的编码格式?????????????????????????????????????????????????????
*/??????????????????????????????????????????????????????????????????????????????
public static String getReomoteURLFileEncode(URL url) {??????????????????????????
?? CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();??????????
?? detector.add(new ParsingDetector(false));??????????????????????????????????????
?? detector.add(JChardetFacade.getInstance());????????????????????????????????????
?? detector.add(ASCIIDetector.getInstance());?????????????????????????????????????
?? detector.add(UnicodeDetector.getInstance());???????????????????????????????????
?? java.nio.charset.Charset charset = null;???????????????????????????????????????
?? try {??????????????????????????????????????????????????????????????????????????
??? System.out.println(url);?????????????????????????????????????????????????????
??? charset = detector.detectCodepage(url);??????????????????????????????????????
?? } catch (Exception ex) {???????????????????????????????????????????????????????
??? ex.printStackTrace();????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
?? if (charset != null) {?????????????????????????????????????????????????????????
??? return charset.name();???????????????????????????????????????????????????????
?? } else {???????????????????????????????????????????????????????????????????????
??? return "utf-8";??????????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
}????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
/**??????????????????????????????????????????????????????????????????????????????
* 获得文件流的编码格式??????????????????????????????????????????????????????????
*/??????????????????????????????????????????????????????????????????????????????
public static String getInputStreamEncode(InputStream is) {??????????????????????
?? CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();??????????
?? detector.add(new ParsingDetector(false));??????????????????????????????????????
?? detector.add(JChardetFacade.getInstance());????????????????????????????????????
?? detector.add(ASCIIDetector.getInstance());?????????????????????????????????????
?? detector.add(UnicodeDetector.getInstance());???????????????????????????????????
?? java.nio.charset.Charset charset = null;???????????????????????????????????????
?? try {??????????????????????????????????????????????????????????????????????????
??? charset = detector.detectCodepage(is, 0);????????????????????????????????????
?? } catch (Exception ex) {???????????????????????????????????????????????????????
??? ex.printStackTrace();????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
?? if (charset != null) {?????????????????????????????????????????????????????????
??? return charset.name();???????????????????????????????????????????????????????
?? } else {???????????????????????????????????????????????????????????????????????
??? return "utf-8";??????????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
}????????????????????????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????????????????
/**??????????????????????????????????????????????????????????????????????????????
* 获得本地文件的编码格式????????????????????????????????????????????????????????
*/??????????????????????????????????????????????????????????????????????????????
public static String getLocalteFileEncode(String filePath) {?????????????????????
?? CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();??????????
?? detector.add(new ParsingDetector(false));??????????????????????????????????????
?? detector.add(JChardetFacade.getInstance());????????????????????????????????????
?? detector.add(ASCIIDetector.getInstance());?????????????????????????????????????
?? detector.add(UnicodeDetector.getInstance());???????????????????????????????????
?? java.nio.charset.Charset charset = null;???????????????????????????????????????
?? File file = new File(filePath);????????????????????????????????????????????????
?? try {??????????????????????????????????????????????????????????????????????????
??? charset = detector.detectCodepage(file.toURI().toURL());?????????????????????
?? } catch (Exception ex) {???????????????????????????????????????????????????????
??? ex.printStackTrace();????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
?? if (charset != null) {?????????????????????????????????????????????????????????
??? return charset.name();???????????????????????????????????????????????????????
?? } else {???????????????????????????????????????????????????????????????????????
??? return "utf-8";??????????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
}????????????????????????????????????????????????????????????????????????????????
/**??????????????????????????????????????????????????????????????????????????????
* 获得字符串的编码格式??????????????????????????????????????????????????????????
*/??????????????????????????????????????????????????????????????????????????????
public static String getStringEncode(String str) {???????????????????????????????
?? CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();??????????
?? detector.add(new ParsingDetector(false));??????????????????????????????????????
?? detector.add(JChardetFacade.getInstance());????????????????????????????????????
?? detector.add(ASCIIDetector.getInstance());?????????????????????????????????????
?? detector.add(UnicodeDetector.getInstance());???????????????????????????????????
?? java.nio.charset.Charset charset = null;???????????????????????????????????????
?? InputStream myIn=new ByteArrayInputStream(str.getBytes());?????????????????????
?? try {??????????????????????????????????????????????????????????????????????????
??? charset = detector.detectCodepage(myIn,3);???????????????????????????????????
?? } catch (Exception ex) {???????????????????????????????????????????????????????
??? ex.printStackTrace();????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
?? if (charset != null) {?????????????????????????????????????????????????????????
??? return charset.name();???????????????????????????????????????????????????????
?? } else {???????????????????????????????????????????????????????????????????????
??? return "utf-8";??????????????????????????????????????????????????????????????
?? }??????????????????????????????????????????????????????????????????????????????
} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?