如何使用Java代码获取文件、文件流或字符串的编码方式

        今天通过网络资源研究了一下如何使用Java代码获取文件、文件流或字符串的编码方式,现将代码与大家分享:

package com.ghj.packageoftool;

import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.ByteOrderMarkDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * 文件工具类
 * 
 * @author 高焕杰
 */
public class FileTool {

	/**
	 * 获取本地文件的编码格式
	 * 
	 * @param file 要判断的文件编码格式
	 * 
	 * @author 高焕杰
	 */
	public static String getLocalFileEncode(File localFile) {

		/*
		 * cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
		 * cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
		 * cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
		 */
		CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
		codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
		codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
		codepageDetector.add(new ByteOrderMarkDetector());  
		codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
		codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
		Charset charset = null;
		try {
			charset = codepageDetector.detectCodepage(localFile.toURI().toURL());
			if (charset != null){
				return charset.name();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获得远程URL文件的编码格式
	 * 
	 * @param url 远程文件的URL路径
	 * 
	 * @author 高焕杰
	 */
	public static String getURLFileEncode(URL url) {

		/*
		 * cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
		 * cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
		 * cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
		 */
		CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
		codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
		codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
		codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
		codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
		Charset charset = null;
		try {
			charset = codepageDetector.detectCodepage(url);
			if (charset != null){
				return charset.name();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获得文件流的编码格式
	 * 
	 * @param inputStream 文件流
	 * 
	 * @author 高焕杰
	 */
	public static String getInputStreamEncode(InputStream inputStream) {

		/*
		 * cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
		 * cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
		 * cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
		 */
		CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
		codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
		codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
		codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
		codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
		Charset charset = null;
		try {
			charset = codepageDetector.detectCodepage(inputStream, 0);
			if (charset != null){
				return charset.name();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获得字符串的编码格式
	 * 
	 * @param stringValue 要判断的文件编码格式字符串
	 * 
	 * @author 高焕杰
	 */
	public static String getStringEncode(String stringValue) {

		/*
		 * cpDetector是探测器,它把探测任务交给具体的探测实现类的实例完成。
		 * cpDetector内置了一些常用的探测实现类,这些探测实现类的实例可以通过add方法 加进来,如ParsingDetector、ByteOrderMarkDetector、JChardetFacade、ASCIIDetector、UnicodeDetector。
		 * cpDetector按照“谁最先返回非空的探测结果,就以该结果为准”的原则返回探测到的字符集编码。cpDetector是基于统计学原理的,不保证完全正确。
		 */
		CodepageDetectorProxy codepageDetector = CodepageDetectorProxy.getInstance();
		codepageDetector.add(new ParsingDetector(false));//ParsingDetector可用于检查HTML、XML等文件或字符流的编码,构造方法中的参数用于指示是否显示探测过程的详细信息,为false不显示。
		codepageDetector.add(JChardetFacade.getInstance());//JChardetFacade封装了由Mozilla组织提供的JChardet,它可以完成大多数文件的编码 测定。所以,一般有了这个探测器就可满足大多数项目的要求,如果你还不放心,可以再多加几个探测器,比如下面的ASCIIDetector、UnicodeDetector等。
		codepageDetector.add(ASCIIDetector.getInstance());//ASCIIDetector用于ASCII编码测定
		codepageDetector.add(UnicodeDetector.getInstance());//UnicodeDetector用于Unicode家族编码的测定
		Charset charset = null;
		try {
			InputStream inputStream = new ByteArrayInputStream(stringValue.getBytes());
			charset = codepageDetector.detectCodepage(inputStream, 3);
			if (charset != null){
				return charset.name();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}

        由于上面代码依赖了很多jar包,所以请直接下载使用MyEclipse开发的Demo。

        【0分下载Demo

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序猿老高

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

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

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

打赏作者

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

抵扣说明:

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

余额充值