- 浏览: 211591 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
Wangwei86609:
非常好的规则引擎框架,支持决策树和多线程运行规则https:/ ...
规则引擎 -
hzxlb910:
真详细,收藏哈
maven setting.xml配置说明 -
东方胜:
[b][/b]
脚本语言 Tcl -
345161974:
hyw520110 写道345161974 写道这个Visua ...
Visual Tcl Binary 完整版(完美中文支持) -
hyw520110:
345161974 写道这个Visual Tcl Binary ...
Visual Tcl Binary 完整版(完美中文支持)
工程项目太多,各工程或各文件编码不统一时,可运行本工具类,把工作目录下,指定类型的文件,转换成指定的编码格式
源码:
import info.monitorenter.cpdetector.io.CodepageDetectorProxy; import info.monitorenter.cpdetector.io.JChardetFacade; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 文本文件转码 * @author Howard.He */ public class TransFileCoding { private static final Logger logger = LoggerFactory.getLogger(TransFileCoding.class); public static void main(String[] args) { String dirPath = "E:/workspace/"; String suffix = ".java"; transformEncoding(dirPath, suffix, "UTF-8"); } /** * 文件转码 (文本文件) * * @param dirPath * 工程路径 * @param suffix * 要转码文件的后缀(.java) * @param newEncoding * 转换的编码 */ public static void transformEncoding(String dirPath, String suffix, String newEncoding) { List<File> list = getFileList(null, dirPath, suffix); if (null != list && !list.isEmpty()) { for (File f : list) { String encoding = getFileCharacterEnding(f); newEncoding = isEmpty(newEncoding) ? getFileCharacterEnding(f) : newEncoding; if (!encoding.equals(newEncoding)) { StringBuilder content = readContent(f, encoding); logger.info("convert file encoding from {} to {}" ,new Object[]{encoding,newEncoding}); write(f, content, newEncoding); } else { logger.debug("srcEncoding({})==dstEncoding({}) skipped f: {}", new Object[] { encoding, newEncoding, f.toString() }); } } } } /** * 取文件的编码格式 * 异常时默认UTF-8 * * @param file * @return */ @SuppressWarnings("deprecation") public static String getFileCharacterEnding(File file) { String fileCharacterEnding = "UTF-8"; CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); detector.add(JChardetFacade.getInstance()); try { Charset charset = detector.detectCodepage(file.toURL()); if (charset != null) fileCharacterEnding = charset.name(); } catch (Exception e) { e.printStackTrace(); } return fileCharacterEnding; } /** * 获取指定路径下的特定类型的文件集 * * @param list * @param dirPath * @param suffix * @return */ public static List<File> getFileList(List<File> list, String dirPath, String suffix) { File dir = new File(dirPath); if (dir.exists()) { File[] files = dir.listFiles(); if (null == list) list = new ArrayList<File>(); for (File f : files) { String name = f.getName(); if (f.isDirectory() && !name.equals(".svn")) getFileList(list, f.getPath(), suffix); else if (f.isFile() && name.endsWith(suffix)) list.add(f); } } return list; } public static StringBuilder readContent(File f, String encoding) { return readContent(f, encoding, "\n"); } /** * 读取文件内容 * * @param f * @param encoding * @return */ public static StringBuilder readContent(File f, String encoding, String enter) { StringBuilder builder = new StringBuilder(); try { encoding = encoding == null ? getFileCharacterEnding(f) : encoding; String data = null; BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), encoding)); while ((data = br.readLine()) != null) { builder.append(isEmpty(enter) ? data : data + enter); } } catch (Exception e) { e.printStackTrace(); } return builder; } /** * 写入文件内容 * * @param f * @param content * @param newEncoding */ private static void write(File f, StringBuilder content, String newEncoding) { try { logger.debug("write file:{}",f); logger.debug(content.toString()); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(f), newEncoding); writer.write(content.toString()); writer.close(); } catch (Exception e) { e.printStackTrace(); } } private static boolean isEmpty(String str) { return null == str || "".equals(str); } }
取文件编码格式,依赖cpdetector ,可到 http://cpdetector.sourceforge.net/ 下载lib包
使用maven时,直接添加依赖信息:
<dependency> <groupId>org.mozilla.intl</groupId> <artifactId>chardet</artifactId> <version>1.4.2</version> <scope>test</scope> </dependency> <dependency> <groupId>info.monitorenter</groupId> <artifactId>cpdetector</artifactId> <version>1.0.7</version> <scope>test</scope> </dependency>
发表评论
-
pushlet
2012-05-31 14:56 1132基于pushlet的文件监控系统的研究与实现 http ... -
@Transactional spring 配置事务
2012-04-25 11:15 2033@Transactional spring 配置事 ... -
Spring的组件自动扫描机制
2012-04-09 17:47 0Spring将所有的bean都纳入到IOC中创建、管理和维护。 ... -
struts&rest
2012-04-03 00:11 749深入浅出REST http://www.infoq. ... -
安装和使用SpringIDE-------III
2011-07-29 10:40 7852. 编写类文件 · ... -
安装和使用SpringIDE-------II
2011-07-29 10:39 650显示图表,如图: 发表于 @ 2006 ... -
安装和使用SpringIDE
2011-07-29 10:36 1093这篇文章谈谈如何安装与使用SpringIDE。作为辅助Sp ... -
使用AJDT简化AspectJ开发
2011-07-29 10:05 974面向方面编程(AOP)可用来解决当今的 许多 应用需求 ... -
利用Apache的CLI来处理命令行
2011-05-16 17:02 952CLI是Jakarta Commons中的一个子类。如果你仅仅 ... -
CGlib简单介绍
2011-04-28 08:37 773CGlib概述:cglib(Code Generation L ... -
Java ClassLoader
2011-04-25 18:24 967当Java编译器编译好.class ... -
Template模式与Strategy模式
2011-04-20 16:23 614template method模式和stra ... -
Ibatis读写CLOB数据
2011-03-21 14:21 986转载:http://www.iteye.com/topic/7 ... -
轻松构建和运行多线程的单元测试
2011-03-18 22:09 934背景 并行程序 并行程序是指控制计算机系统中两个或多个分别 ... -
Cairngorm3中文简介
2011-03-18 22:07 969官方原文地址:http://opensource.adobe. ... -
ibator改造之返回数据库注释和数据库分页
2010-12-23 17:24 2185转载:http://www.iteye.com ... -
quatrz 任务监控管理 (2)
2010-10-28 23:28 1410在《Quartz 任务监控管理 (1)》http://www. ... -
Quartz任务监控管理 (1)
2010-10-28 23:27 1226转载:http://sundoctor.iteye.com/b ... -
Quartz 在 Spring 中如何动态配置时间
2010-10-28 23:25 1625转载: http://sundoctor.iteye.com ... -
使用org.apache.commons.net.ftp包开发FTP客户端,实现进度汇报,实现断点续传,中文支持
2010-10-28 21:09 970使用org.apache.commons.net.ftp包开发 ...
相关推荐
实际项目中有很多文件的编码是非utf编码,当多种编码的文件相互包含时,会出现乱码。 应实际需求,特写一个java程序,将非utf8编码的文件,转换为utf8编码。省却了一个文件一个文件修改的...可快速批量的见文件转码。
天翼网关F452配置文件转码软件
批量文件转码工具,支持GBK,UTF-8转换,window下面使用,亲测可以使用!!!!!!!!!!
批量文件转码工具,支持GBK,UTF-8转换,window下面使用,亲测可以使用!
监控视频.dav文件转码工具,支持转换为多种格式(MP4、AVI、WMV、MXF、GIF、DPG、MTV、AMV、SWF等多种格式,应有尽有。)。本软件为英文版软件,没有中文版,介意的话不要下载,避免浪费积分。也可当播放器使用,...
批量文件转码工具 批量文件转码工具 批量文件转码工具 批量文件转码工具 批量文件转码工具
比如图2的转码规则就是对项目的jsp文件,不管原来是什么编码,统一转为UTF-8编码,并且同时转换文件内容。可以点击右边的Add /Remove按钮来增加/删除规则。最后点击Apply来保存设置。 step3. 转码 在设置了转码规...
对文件上传预览,这里涉及到文件转码(word、ppt、图片、Excal)
批量文件转码工具(支持 GBK,UTF-8 转换)。 批量文件转码工具,支持GBK,UTF-8转换,window下面使用,亲测可以使用!!!!!!!!!! 批量 文件转码 工具 GBK UTF-8
MFC界面实现文件转码,目前支持Unicode、ANSI、UTF8三种编码格式相互转化,利用C++多态性质采用工厂模式,便于扩展更多编码格式;部分采用多线程
mysql命令行直接倒出数据,然后转换为csv文件,编码自动转换,支持windows
多文件转码小程序,linux文件夹拷贝到windows下,往往会出现乱码,此程序可帮您批量进行转码
获取文件编码格式与文件转码,提供几个工具方法
音频文件转码java开发,基于jave开源技术。 音频文件转码java开发,基于jave开源技术。
批处理文件转码包iconv.exe
获取文件编码格式与文件转码.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
MKV视频文件提取音频DTS文件转码AC3后封装方法.txt
炫酷乐转码先生是北京坤腾世纪科技有限公司面向个人和家庭用户推出的一款免费软件产品,它可以将各种来源的音视频文件转换成各种不同的格式,而且转码质量稳定,支持批量转码,速度快,可称之为“万能转码器”。...