Java编程实现文件编码转换
来源:互联网 发布:淘宝运营需要学美工吗 编辑:程序博客网 时间:2024/06/05 08:26
最近在做自然语言处理的工程,数据源是由各个工作人员编写汇报上来的中文文本,存在文本编码不统一的问题。借助cpDetector工具自动识别文本的编码,编写函数JudgeFileCode,并实现文本编码的转换,统一编码为UTF-8,写个博客保存一下。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import info.monitorenter.cpdetector.io.ASCIIDetector;
import info.monitorenter.cpdetector.io.CodepageDetectorProxy;
import info.monitorenter.cpdetector.io.JChardetFacade;
import info.monitorenter.cpdetector.io.ParsingDetector;
import info.monitorenter.cpdetector.io.UnicodeDetector;
public class FileCharsetConverter {
private static void convert(File file, String fromCharsetName, String toCharsetName, FilenameFilter filter)throws Exception {
// TODO Auto-generated method stub
if(file.isDirectory()) {
File[] fileList=null;
if(filter==null) {
fileList=file.listFiles();
}else {
fileList=file.listFiles(filter);
}
for(File f:fileList) {
convert(f,fromCharsetName,toCharsetName,filter);
}
}else {
if(filter==null||filter.accept(file.getParentFile(), file.getName())) {
String fileContent=getFileContentFromCharset(file,fromCharsetName);
saveFile2Charset(file,toCharsetName,fileContent);
}
}
}
//以指定编码方式写文本文件,存在会覆盖
private static void saveFile2Charset(File file, String toCharsetName, String content)throws Exception {
// TODO Auto-generated method stub
if(!Charset.isSupported(toCharsetName)) {
throw new UnsupportedCharsetException(toCharsetName);
}
FileOutputStream outputStream=new FileOutputStream(file);
OutputStreamWriter outWrite=new OutputStreamWriter(outputStream,toCharsetName);
outWrite.write(content);
outWrite.close();
}
//以指定编码读取文件,返回文件内容
private static String getFileContentFromCharset(File file, String fromCharsetName)throws Exception {
// TODO Auto-generated method stub
if(!Charset.isSupported(fromCharsetName)) {
throw new UnsupportedCharsetException(fromCharsetName);
}
FileInputStream inputStream=new FileInputStream(file);
InputStreamReader reader=new InputStreamReader(inputStream,fromCharsetName);
char[] chs=new char[(int)file.length()];
reader.read(chs);
String str=new String(chs).trim();
reader.close();
return str;
}
private static String JudgeFileCode(File file) {
CodepageDetectorProxy detector=CodepageDetectorProxy.getInstance();
detector.add(new ParsingDetector(false));
detector.add(JChardetFacade.getInstance());
detector.add(ASCIIDetector.getInstance());
detector.add(UnicodeDetector.getInstance());
Charset charset=null;
try {
charset=detector.detectCodepage(file.toURL());
}catch(Exception ex) {
ex.printStackTrace();
}
if(charset!=null) {
return charset.name();
}else {
return null;
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File srcfile=new File("sourceText");
File[] sfiles=srcfile.listFiles();
for(File sfile:sfiles) {
String code=JudgeFileCode(sfile);
//System.out.println(code);
convert(sfile,code,"utf-8",new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith("txt");
}
});
}
}
}
参考博客:http://weict1988.iteye.com/blog/1003379
http://blog.csdn.net/wfdtxz/article/details/26166853
http://blog.csdn.net/mhmyqn/article/details/37917947#java
- Java编程实现文件编码转换
- 通过Java实现文件的编码转换
- java 文件编码转换
- JAVA 文件编码转换
- java文件编码转换
- JAVA 文件编码转换
- java 文件编码转换
- Java实现GBK编码文件繁简体转换
- java文件编码格式转换
- Java批量转换文件编码
- Java IO文件编码转换
- Java文件编码格式转换
- EditPlus实现文件编码批量转换
- java获取文件编码实现
- Java版文件编码转换工具
- Java native2ascii工具 properties文件编码转换
- Java native2ascii工具 properties文件编码转换
- Java字符编码转换与文件加锁
- Vue2.0入门
- 常见排序算法的复杂度以及稳定性
- 图片轮播的功能实现
- 选择排序的递归算法
- 解决CentOS 7,ATI显卡,屏幕亮度调节问题
- Java编程实现文件编码转换
- C#基础(2)——字符操作
- 今年最新的30个Android库
- BurpSuite安全测试
- ANDROID样式的开发:LAYER-LIST篇
- Java线程(一):synchronized
- 修改hosts不必重启 立刻生效
- Task Scheduler
- AlphaGo Zero的伟大与局限—ResNet作者、旷视研究院院长孙剑深度评述