六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

Java获得指定文件的编码类型

[复制链接]

升级  12.67%

17

主题

17

主题

17

主题

秀才

Rank: 2

积分
69
 楼主| 发表于 2013-1-26 13:35:18 | 显示全部楼层 |阅读模式
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;import info.monitorenter.cpdetector.io.JChardetFacade;import java.io.File;  import java.nio.charset.Charset;  /** * 获得文件的编码类型 * 需要三个jar包:antlr.jar,chardet.jar,cpdetector_1.0.7.jar * */public class CharacterEnding {  public static void main(String[] args) {new CharacterEnding().getFileCharacterEnding("C://temps//zip压缩");  }/** * 获得文件的编码类型 * @param filePath * @return */public String getFileCharacterEnding(String filePath ) {  String fileCharacterEnding = "UTF-8";  CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();  detector.add(JChardetFacade.getInstance());  Charset charset = null;  File file = new File(filePath);  try {  charset = detector.detectCodepage(file.toURL());  } catch (Exception e) {  e.printStackTrace();  }  if (charset != null) {  fileCharacterEnding = charset.name();System.out.println(filePath+"的编码类型为:"+fileCharacterEnding);}  return fileCharacterEnding;  }  }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表