使用 jchardet 获得文件编码

//使用 jchardet 获得文件编码 -javacode
//当含中文的文件用ANSI编码保存时,检测还是出错。

package org.mozilla.intl.chardet;

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

/**
* 借助JCharDet获取文件字符集
* @author icer
* PS:
* JCharDet 是mozilla自动字符集探测算法代码的java移植,其官方主页为:
* http://jchardet.sourceforge.net/
* @date 2008/11/13
*/
public class FileCharsetDetector {

private boolean found = false;

/**
* 如果完全匹配某个字符集检测算法, 则该属性保存该字符集的名称. 否则(如二进制文件)其值就为默认值 null, 这时应当查询属性
*/
private String encoding = null;

public static void main(String[] argv) throws Exception {
if (argv.length != 1 && argv.length != 2) {

System.out
.println("Usage: FileCharsetDetector <path> [<languageHint>]");

System.out.println("");
System.out.println("Where <path> is d:/demo.txt");
System.out.println("For optional <languageHint>. Use following...");
System.out.println(" 1 => Japanese");
System.out.println(" 2 => Chinese");
System.out.println(" 3 => Simplified Chinese");
System.out.println(" 4 => Traditional Chinese");
System.out.println(" 5 => Korean");
System.out.println(" 6 => Dont know (default)");

return;
} else {
String encoding = null;
if (argv.length == 2) {
encoding = new FileCharsetDetector().guestFileEncoding(argv[0],
Integer.valueOf(argv[1]));
} else {
encoding = new FileCharsetDetector().guestFileEncoding(argv[0]);
}
System.out.println("文件编码:" + encoding);
}
}

/**
* 传入一个文件(File)对象,检查文件编码
*
* @param file
* File对象实例
* @return 文件编码,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(File file) throws FileNotFoundException,
IOException {
return geestFileEncoding(file, new nsDetector());
}

/**
* 获取文件的编码
*
* @param file
* File对象实例
* @param languageHint
* 语言提示区域代码 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese;
* 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default)
* @return 文件编码,eg:UTF-8,GBK,GB2312形式,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(File file, int languageHint)
throws FileNotFoundException, IOException {
return geestFileEncoding(file, new nsDetector(languageHint));
}

/**
* 获取文件的编码
*
* @param path
* 文件路径
* @return 文件编码,eg:UTF-8,GBK,GB2312形式,若无,则返回null
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(String path) throws FileNotFoundException,
IOException {
return guestFileEncoding(new File(path));
}

/**
* 获取文件的编码
*
* @param path
* 文件路径
* @param languageHint
* 语言提示区域代码 eg:1 : Japanese; 2 : Chinese; 3 : Simplified Chinese;
* 4 : Traditional Chinese; 5 : Korean; 6 : Dont know (default)
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public String guestFileEncoding(String path, int languageHint)
throws FileNotFoundException, IOException {
return guestFileEncoding(new File(path), languageHint);
}

/**
* 获取文件的编码
*
* @param file
* @param det
* @return
* @throws FileNotFoundException
* @throws IOException
*/
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;
}
}
Jchardet——支持检测并输出文件编码方式的组件 Jchardet是OpenAtom OpenHarmony(以下简称“OpenHarmony”)系统的一款检测文本编码的组件。当上传一个文件时,组件可以检测并输出该文件中文本使用的编码方式。 阅读详情

相关推荐

chardet-1.0.jar

<dependency> <groupId>org.mozilla.intl.chardet</groupId> <artifactId>chardet</artifactId> <version>1.0</version> </dependency>

获取文件编码chardet

package org.wanda.work.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.I

新嘉庄的博客 1306

mozilla检测网页编码的jar包

mozilla检测网页编码的jar包,年代较为久远,但实测可用

文件编码识别与编码格式转换 - java 源码

Java 实现文件编码识别、编码格式转换功能。

一步一步往上爬 1709

org.mozilla.universalchardet.jar

org.mozilla.universalchardet.jar org.mozilla.universalchardet.jar

大模型推理常见采样策略:Top-k, Top-p, Temperature, Beam Search

大模型推理采样策略,举例通俗易懂汇总介绍

qq_43243579的博客 2万+

字符集编码的自动识别jchardet

什么是jchardet? jchardet是mozilla自动字符集探测算法代码的java移植,其源代码可以从sourceforge下载。这个算法的最初作者是 frank Tang,C++源代码在http://www.infomall.cn/cgi-bin/mallgate/20040514/http: //lxr.mozilla.org/mozilla/source/intl/ch...

xiangxingchina 303

RcisTarget实战:从差异基因到转录因子调控网络的完整分析流程(附代码)

本文详细介绍了RcisTarget工具在转录因子调控网络分析中的完整流程,从差异基因到motif富集分析,再到结果可视化与生物学解读。通过实战代码演示,帮助研究者掌握这一强大的R包应用,揭示基因表达背后的调控机制,特别适用于基因组学和生物信息学研究。

weixin_29214199的博客 228

使用JCharDet,获得文件的字符集编码!

需要jchardet-1.0.jar包。   package p; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; imp

guojg的专栏 4718

雷达信号处理——恒虚警检测(CFAR)

上图展示的是同时在距离维和多普勒维进行恒虚警检测的原理,如果只是在距离维或多普勒维做CFAR,检测单元的左右两侧均有设定好数量的保护单元和参考单元。不同的CFAR检测器适用于不同的场景,CA-CFAR算法适用于均匀环境中的单目标或彼此相距较远的多目标环境,在邻近多目标环境会发生目标的遮蔽效应,在杂波边缘会发生杂波边缘效应。从图中也可以看到,根据阈值的选择不同,有不同的检测器,其中CA-CFAR是取的参考单元的均值,SO-CFAR取的是参考单元的最小值,而GO-CFAR是取的参考单元的最大值。

诚朴勇毅 3万+

手把手教你为FPGA项目集成HyperRAM IP核:从AXI接口封装到上板调试全流程

本文详细介绍了如何为FPGA项目集成HyperRAM IP核,涵盖从AXI接口封装到上板调试的全流程。通过对比DDR3L、PSRAM和HyperRAM的性能指标,突出HyperRAM在低功耗和小封装尺寸方面的优势,并提供硬件连接、AXI4-Lite接口设计、状态机优化及调试技巧等实用内容,帮助开发者高效完成FPGA项目中的内存扩展。

weixin_32103009的博客 286

java jchardet_使用 jchardet 获得文件编码 -javacode

//使用 jchardet 获得文件编码 -javacode//当含中文的文件用ANSI编码保存时,检测还是出错。package org.mozilla.intl.chardet;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFo...

weixin_33462050的博客 297

新卡尝鲜记:RTX 5070Ti适配PyTorch生态的实战避坑指南

本文分享了RTX 5070Ti适配PyTorch生态的实战经验。针对CUDA 12.8兼容性问题,详细介绍了安装PyTorch Nightly版本、手动编译xformers库以及寻找PyTorch3D第三方预编译包的完整流程与解决方案,帮助用户快速搭建可用的深度学习开发环境。

weixin_29094775的博客 289

使用 jchardet 获得文件编码 -javacode

转载自:http://hi.baidu.com/hxzon/blog/item/f2b8b51bfa5101108618bfd1.html //使用 jchardet 获得文件编码 -javacode //当含中文的文件用ANSI编码保存时,检测还是出错。 package org.mozilla.intl.chardet; import java.io.BufferedInputStream

客亦知夫水与月乎? 1545

VerilogEval实战指南:从基准测试到模型微调的全流程解析

本文提供了VerilogEval实战指南,详细解析了从基准测试到模型微调的全流程。VerilogEval是一个自动化、标准化的测试框架,用于客观评估大语言模型生成Verilog硬件代码的能力。文章涵盖了环境搭建、核心机制解析、利用其生成合成数据进行模型微调的策略,以及如何将其工程化集成到自动化评估流程中,为硬件设计领域的AI应用提供了完整的方法论。

yy01234的博客 1005

agent-browser 安装笔记(OpenClaw)

【代码】agent-browser 安装笔记(OpenClaw)

tiantiantbtb的博客 1242

jChardet探测文件字符编码

package test; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.mozilla.intl.charde

jethai的博客 699

【CSP-S 提高组】考试大纲集合

【CSP-S提高组】考试大纲集合

weixin_42596089的博客 7257

OpenHarmony文本编码检测组件——Jchardet

Jchardet是OpenAtom OpenHarmony(以下简称“OpenHarmony”)系统的一款检测文本编码的组件。当上传一个文件时,组件可以检测并输出该文件中文本使用的编码方式。

xianglizaibie的博客 937

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

OpenHarmony实战——Jchardet文件编码方式组件

Jchardet是OpenAtom OpenHarmony(以下简称“OpenHarmony”)系统的一款检测文本编码的组件。当上传一个文件时,组件可以检测并输出该文件中文本使用的编码方式。

adaedwa187545的博客 664

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

java jchardet_借助JCharDet获取文件字符集

packagecom.zhyea.util;importjava.io.BufferedInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importorg.mozilla.intl.chardet.ns...

weixin_39900676的博客 218

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

jchardet自动检测文件编码

pom配置 &lt;!--enCode--&gt; &lt;dependency&gt; &lt;groupId&gt;net.sourceforge.jchardet&lt;/groupId&gt; &lt;artifactId&gt;jchardet&lt;/artifactId&gt; &lt;ver...

放开那个桃子的博客 821

java 探测 txt字符编码,jChardet探测文件字符编码

packagetest;importjava.io.BufferedInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importorg.mozilla.intl.chardet.nsDet...

weixin_36154762的博客 251
上一篇: 修改官方Twitter For Android,自定义 API
下一篇: StatusBar (状态栏)的架构(Android 2.3)(转)
sharp2wing
博客等级 码龄19年 5粉丝 89原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值