Java判断文件编码

首先,我们需要三个jar包

主要jar包:cpdetector.jar
同时还需jchardet-1.0.jar这个包,否则detector.add(cpdetector.io.JChardetFacade.getInstance());  会报错;
还有一个antlr.jar,不然运行过程中detector.add(new ParsingDetector(false));会报错;

如图所示

public class Utils {
	private static final String UNKONW_CHARSET = "unknow charset";

	@SuppressWarnings("deprecation")
	public static String getFileCharSet(File file) {
		/**
		 * CodepageDetectorProxy是探测器,它把探测任务交给具体的探测实例完成。
		 * CodepageDetectorProxy内置了一些常用的探测实现类。
		 * 这些探测实现类的实例可以通过add方法 加进来
		 * 如ParsingDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
		 * CodepageDetectorProxy按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的 字符集编码。
		 */
		CodepageDetectorProxy detectorProxy = CodepageDetectorProxy.getInstance();
		/**
		 * ParsingDetector
		 * 	可用于检查HTML、XML等文件或字符流的编码
		 * 	构造方法中的参数用于指示是否显示探测过程的详细信息
		 * 	为false不显示。
		 */
		//如果要探测xml,html文件本身的编码,而不是探测xml的encoding,请注释掉(html同理)
		detectorProxy.add(new ParsingDetector(false));
		/**
		 *   JChardetFacade
		 *   	封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码测定。
		 *   	所以,一般有了这个探测器就可满足大多数项目的要求。
		 *   	如果你还不放心,可以再多加几个探测器
		 *   	比如下面的ASCIIDetector、UnicodeDetector等。  
		 */
		detectorProxy.add(JChardetFacade.getInstance());
		//UnicodeDetector用于Unicode家族编码的测定
		detectorProxy.add(UnicodeDetector.getInstance());
		//ASCIIDetector用于ASCII编码测定
		detectorProxy.add(ASCIIDetector.getInstance());
		Charset charset = null;
		try {
			//注意,当文件名称中有特殊符号,例如%的时候,本句代码抛异常
			charset = detectorProxy.detectCodepage(file.toURL());
			return charset.name();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(file.getAbsolutePath() + " unknow charset");
		}
		return UNKONW_CHARSET;
	}

}

 

Java中,判断文件编码通常涉及到读取文件内容并尝试解码,如果解码失败,可以推测文件可能是使用了不同的字符集。以下是一个简单的示例,使用`BufferedReader`配合异常处理来猜测文件编码: ```java import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.charset.Charset; public class Main { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt"), Charset.defaultCharset()))) { String line; while ((line = reader.readLine()) != null) { // 如果这里解码成功,说明默认字符集就是正确的 System.out.println(line); } } catch (UnsupportedEncodingException e) { // 尝试其他常见编码,如UTF-8、GBK等 try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt"), "UTF-8"))) { // 如果这里能正常读取,则可能使用的是UTF-8 } catch (Exception ex) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("file.txt"), "GBK"))) { // 同理,如果GBK可以,那就使用GBK } catch (Exception ex2) { // 如果所有尝试都失败,打印错误信息 System.err.println("Failed to determine the encoding, assuming default or system-specific encoding."); } } } catch (IOException e) { e.printStackTrace(); } } } ``` 这个例子首先尝试使用系统默认的字符集,如果遇到`UnsupportedEncodingException`,则逐个尝试常见的字符集。实际应用中,可能需要根据具体情况进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试的自我修养

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值