在读取文本文件时,通常需要先获取文件的编码集。

可以采用

  /**
   * @description 获取文件编码集
   * @date Jul 21, 2011
   * @param file 文件对象
   * @return 文件编码集名称
   */
  public static String getFileCharacterEnding(File file) {

    String fileCharacterEnding = "UTF-8";
    info.monitorenter.cpdetector.io.CodepageDetectorProxy detector = CodepageDetectorProxy
        .getInstance();
    detector.add(JChardetFacade.getInstance());

    Charset charset = null;
    try {
      charset = detector.detectCodepage(file.toURL());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    if (charset != null) {
      fileCharacterEnding = charset.name();
    }

    return fileCharacterEnding;
  }

本代码需要两个jar包

cpdetector_1.0.8.jar和chardet-1.0.jar

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
  • 2021-11-25
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-11
相关资源
相似解决方案