读取文件编码

本文详细探讨了如何正确地读取不同编码格式的文件,包括常见字符集的使用和处理,帮助读者掌握文件编码读取的关键技巧。
摘要由CSDN通过智能技术生成

读取文件编码

package com.tool.charset;

import java.io.BufferedInputStream;   
import java.io.File;   
import java.io.FileInputStream;   
import java.io.FileNotFoundException;   
import java.io.IOException;   

import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
  
/**  
 * 功能:读取文件编码
 *      http://jchardet.sourceforge.net/  
 */  
public class FileCharsetDetector {   
  
    private boolean found = false;   
  
    private String encoding = null;   
  
    public static void main(String[] argv) throws Exception, IOException{
        
    	String fileName = "D:\\key.ok.dat";
    	
    	String encoding = null;   
        if (argv.length == 2) {   
            encoding = new FileCharsetDetector().guestFileEncoding(fileName, 1);   
        } else {   
            encoding = new FileCharsetDetector().guestFileEncoding(fileName);   
        }   
        System.out.println("文件编码:" + encoding);  
        
    }

    public String guestFileEncoding(File file) throws FileNotFoundException,   
            IOException {   
        return geestFileEncoding(file, new nsDetector());   
    }   
  
    public String guestFileEncoding(File file, int languageHint)   
            throws FileNotFoundException, IOException {   
        return geestFileEncoding(file, new nsDetector(languageHint));   
    }   
  
    public String guestFileEncoding(String path) throws FileNotFoundException,   
            IOException {   
        return guestFileEncoding(new File(path));   
    }   
  
    public String guestFileEncoding(String path, int languageHint)   
            throws FileNotFoundException, IOException {   
        return guestFileEncoding(new File(path), languageHint);   
    }   
  
    private String geestFileEncoding(File file, nsDetector det)   
            throws FileNotFoundException, IOException {   
        // Set an observer...   
        // The Notify() will be called when a matching charset is found.   
        det.Init(new nsICharsetDetectionObserver() {   
            public void Notify(String charset) {   
                found = true;   
                encoding = charset;   
            }   
        });   
  
        BufferedInputStream imp = new BufferedInputStream(new FileInputStream(   
                file));   
  
        byte[] buf = new byte[1024];   
        int len;   
        boolean done = false;   
        boolean isAscii = true;   
  
        while ((len = imp.read(buf, 0, buf.length)) != -1) {   
            // Check if the stream is only ascii.   
            if (isAscii)   
                isAscii = det.isAscii(buf, len);   
  
            // DoIt if non-ascii and not done yet.   
            if (!isAscii && !done)   
                done = det.DoIt(buf, len, false);   
        }   
        det.DataEnd();   
  
        if (isAscii) {   
            encoding = "ASCII";   
            found = true;   
        }   
  
        if (!found) {   
            String prob[] = det.getProbableCharsets();   
            if (prob.length > 0) {
                encoding = prob[0];   
            } else {   
                return null;   
            }   
        }   
        return encoding;   
    }   
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值