Java获取文件编码格式

该博客介绍了一个Java工具类`FileCharsetCheckUtils`,用于检测文件的字符集。通过使用`cpdetector`库,该类能够识别输入流或文件的编码类型,包括ASCII、Unicode等,并提供了一个简单的示例来读取和打印文件内容。文章还提供了`cpdetector`的下载链接以及如何在Maven项目中引入依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import info.monitorenter.cpdetector.io.*;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class FileCharsetCheckUtils {

    public static void main(String[] args) throws IOException {
        File file = null;
        BufferedReader bufferedReader = null;
        try {
            //如果能得到File对象,直接传入getFileCharset方法即可,如果只知道InputStream流信息,则如下
            InputStream is = new FileInputStream(new File("C:\\Users\\Admin\\Desktop\\ageing.data"));
            file = File.createTempFile("temp", ".txt");//创建临时文件
            Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
            String filetype = getFileCharset(file);
            //此时 is 中的内容是空的
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), filetype));
            System.out.println(bufferedReader.readLine());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (file != null && bufferedReader != null) {
                bufferedReader.close();
                file.deleteOnExit();
            }
        }
    }

    public static String getFileCharset(File file) throws Exception {
        CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance();
        detector.add(new ParsingDetector(false));
        detector.add(JChardetFacade.getInstance());
        detector.add(ASCIIDetector.getInstance());
        detector.add(UnicodeDetector.getInstance());
        Charset charset = null;
        try {
            charset = detector.detectCodepage(file.toURI().toURL());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        String charsetName = "GBK";
        if (charset != null) {
            if (charset.name().equals("US-ASCII")) {
                charsetName = "ISO_8859_1";
            } else if (charset.name().startsWith("UTF")) {
                charsetName = charset.name();// 例如:UTF-8,UTF-16BE.
            }
        }
        return charsetName;
    }
}
cpdetector所需Jar包

优快云下载地址:https://download.youkuaiyun.com/download/xll_csdn/12923009

百度网盘下载地址:https://pan.baidu.com/s/1MIyOg3wjVcT-iICvPL6owg 提取码:fu6o 

如需maven引入,则将压缩包直接解压到本地maven仓库根目录,如图

  pom.xml引入

<dependency>
    <groupId>cpdetector</groupId>
    <artifactId>cpdetector</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>cpdetector</groupId>
    <artifactId>antlr</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>cpdetector</groupId>
    <artifactId>chardet</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>cpdetector</groupId>
    <artifactId>jargs</artifactId>
    <version>1.0</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值