java 如何判断txt的编码字符集

Hello5230 2013-03-27 03:24:34
最近在做一个文本阅读器,显示时需要获得本地导入的txt的编码字符集,网上搜了,都不行,还在搜,请高手给个成熟的方法,谢谢了。在线狂等中.
我的代码(不可用):
private static void judgeTextCode(String strFilePath) {
FileInputStream fis = null;
try {
fis = new FileInputStream(strFilePath);
int a = fis.read();
int b = fis.read();
if(a==0xFF&&b==0xFE) {
System.out.println("----------Unicode------");
}
else if(a==0xFE&&b==0xFF) {
System.out.println("----------UTF-16BE------");
}
else if(a==0xEF&&b==0xBB) {
System.out.println("----------UTF-8------");
} else {
System.out.println("----------GBK------");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(fis != null) {
fis.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}

}
...全文
668 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
orisun 2013-06-05
  • 打赏
  • 举报
回复
是呵,我现在做项目,也遇到这个问题,你解决了吗?
引用 8 楼 windows771053651 的回复:
我用了个开源的探测器方法: public static void getCharset(String path) { CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); detector.add(new ParsingDetector(false)); detector.add(ASCIIDetector.getInstance()); detector.add(UnicodeDetector.getInstance()); java.nio.charset.Charset charset = null; File f=new File(path); try { charset = detector.detectCodepage(new BufferedInputStream(new FileInputStream(f)),100); } catch (Exception ex) {ex.printStackTrace();} if(charset!=null){ System.out.println(f.getName()+"编码是:"+charset.name()); }else{ System.out.println(f.getName()+"未知"); } } 但总是报:java.lang.IllegalArgumentException: More than the given length had to be read and the given stream could not be reset. Undetermined state for this detection. at info.monitorenter.cpdetector.io.CodepageDetectorProxy.detectCodepage(CodepageDetectorProxy.java:198) 谁用过这个探测器啊,指点下
sdfhejian520 2013-03-29
  • 打赏
  • 举报
回复
byte[] head = new byte[3]; inputStream.read(head); String code = "gbk"; if (head[0] == -1 && head[1] == -2){ code = "UTF-16"; } if (head[0] == -2 && head[1] == -1){ code = "Unicode"; } if (head[0] == -17 && head[1] == -69 && head[2] == -65){ code = "UTF-8"; }
Hello5230 2013-03-29
  • 打赏
  • 举报
回复
在网上搜到一个方法,探测各种文件(html,txt,ascii)字符集都可以:http://blog.sina.com.cn/s/blog_80e4822f0101dd0s.html
Hello5230 2013-03-27
  • 打赏
  • 举报
回复
我用了个开源的探测器方法: public static void getCharset(String path) { CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); detector.add(new ParsingDetector(false)); detector.add(ASCIIDetector.getInstance()); detector.add(UnicodeDetector.getInstance()); java.nio.charset.Charset charset = null; File f=new File(path); try { charset = detector.detectCodepage(new BufferedInputStream(new FileInputStream(f)),100); } catch (Exception ex) {ex.printStackTrace();} if(charset!=null){ System.out.println(f.getName()+"编码是:"+charset.name()); }else{ System.out.println(f.getName()+"未知"); } } 但总是报:java.lang.IllegalArgumentException: More than the given length had to be read and the given stream could not be reset. Undetermined state for this detection. at info.monitorenter.cpdetector.io.CodepageDetectorProxy.detectCodepage(CodepageDetectorProxy.java:198) 谁用过这个探测器啊,指点下
Hello5230 2013-03-27
  • 打赏
  • 举报
回复
可是还是不行阿,记得网上有个探测器什么的
  • 打赏
  • 举报
回复
else { code = "GBK"; } GBK改为ANSI
Hello5230 2013-03-27
  • 打赏
  • 举报
回复
可是有篇txt,本来是UTF-8的,用上面方法得到的是GBK,出现乱码,但是用ireader读就没问题
  • 打赏
  • 举报
回复
txt就四种编码。
Hello5230 2013-03-27
  • 打赏
  • 举报
回复
上面的不行啊,有些txt判断正确,有些不正确
u010055153 2013-03-27
  • 打赏
  • 举报
回复
65465461313113131313
  • 打赏
  • 举报
回复


public static void main(String[] args) {
		try {
			String charset = getCharset(new File("c:\\2.txt"));
			System.out.println(charset);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	 public static String toHex(byte[] byteArray) {
	        int i;
	        StringBuffer buf = new StringBuffer("");
	        int len = byteArray.length;
	        for (int offset = 0; offset < len; offset++) {
	            i = byteArray[offset];
	            if (i < 0)
	                i += 256;
	            if (i < 16)
	                buf.append("0");
	            buf.append(Integer.toHexString(i));
	        }
	        return buf.toString().toUpperCase();
	    }
	  private static String getCharset(File fileName) throws IOException {
	        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
	        byte[] b = new byte[10];
	        bin.read(b, 0, b.length);
	        String first = toHex(b);
	 //这里可以看到各种编码的前几个字符是什么,gbk编码前面没有多余的
	        String code = null;
	        if (first.startsWith("EFBBBF")) {
	            code = "UTF-8";
	        } else if (first.startsWith("FEFF00")) {
	            code = "UTF-16BE";
	        } else if (first.startsWith("FFFE")) {
	            code = "Unicode";
	        } else if (first.startsWith("FFFE")) {
	            code = "Unicode";
	        } else {
	            code = "GBK";
	        }
	        return code;
	    }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧