栏目导航
热点推荐
- 三十条有用的 Java 编程规则
- Java制作水印图片源码
- Java常见异常及可能的导致原因
- Java中的修饰词使用方法总结
- J2EE系统异常的处理准则
- Java中的异常、断言、日志解析(
- Java面试技巧:Java面试题集锦(
- 面向Java开发人员的Scala指南:
- Java程序员:一刻钟精通正则表达
- 网友经验分享:学好java开发的关
- 专家解答:创建表格与数据库进行
- Java远程访问Domino数据库
阅览排行
Java 获得文件编码格式的代码
www.jz123.cn 2009-12-10 来源: 中国建站 责任编辑(袁袁) 我要投递新闻
Java代码
import info.monitorenter.cpdetector.io.CodepageDetectorProxy; import info.monitorenter.cpdetector.io.JChardetFacade; import java.io.File; import java.nio.charset.Charset; public class CharacterEnding { public static String getFileCharacterEnding(String filePath) { File file = new File(filePath); return getFileCharacterEnding(file); } /** * Try to get file character ending. Warning: use * cpDetector to detect file's encoding. * * @param file * @return */ public static String getFileCharacterEnding(File file) { String fileCharacterEnding = "UTF-8"; CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); detector.add(JChardetFacade.getInstance()); Charset charset = null; // File f = new File(filePath); try { charset = detector.detectCodepage(file.toURL()); } catch (Exception e) { e.printStackTrace(); } if (charset != null) { fileCharacterEnding = charset.name(); } return fileCharacterEnding; } public static void main(String[] args) { String filePath = "d:\t.txt"; String type = CharacterEnding.getFileCharacterEnding(filePath); System.out.println(type); } } |
0
上一篇:Java中Collection、List的使用 下一篇:Java23种设计模式中常用的九种模式介绍