已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java 文件编码转换 package com.folkSeal.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.nio.charset.Charset;/* IO 工具类 */public class IOCVUtils /* 源文件编码 */public static String sourceEncoding = GBK;/* 目标编码 */public static String targetEncoding = UTF-8;/* * 文件内容转编码 * param sourceFile * param targetFile * throws UnsupportedEncodingException * throws FileNotFoundException * throws IOException */public static void changeEncoding(File sourceFile, File targetFile)throws UnsupportedEncodingException, FileNotFoundException,IOException FileInputStream fin = null;FileOutputStream fout = null;FileChannel fcin = null;FileChannel fcout = null;if (sourceEncoding = null) IOCVUtils.sourceEncoding = System.getProperty(file.encoding);try fin = new FileInputStream(sourceFile);fout = new FileOutputStream(targetFile);fcin = fin.getChannel();fcout = fout.getChannel();ByteBuffer buffer = ByteBuffer.allocateDirect(1024);while (true) buffer.clear();int r = fcin.read(buffer);if (r = -1) break;buffer.flip();fcout.write(ByteBuffer.wrap(Charset.forName(sourceEncoding).decode(buffer).toString().getBytes(targetEncoding); finally if (fin != null) fin.close();fin = null;if (fcin != null) fcin.close();fcin = null;if (fout != null) fout.close();fout = null;if (fcout != null) fcout.close();fcout = null;/* * 文件内容转编码 * param sourceFile * param targetFile * throws UnsupportedEncodingException * throws FileNotFoundException * throws IOException */public static void changeEncoding(String sourceFile, String targetFile) throws UnsupportedEncodingException, FileNotFoundException, IOExceptionFile fl1 = new File(sourceFile);File fo1 = new File(targetFile);changeEncoding(fl1, fo1);/* * 文件内容转编码 * param sourceFile * param targetFile * param sourceEncoding 源文件编码 默认源文件的系统存储编码 System.getProperty(file.encoding); * param targetEncoding 目标编码 默认utf-8 * throws UnsupportedEncodingException * throws FileNotFoundException * throws IOException */public static void changeEncoding(String sourceFile, String targetFile,String sourceEncoding, String targetEncoding) throws UnsupportedEncodingException, FileNotFoundException, IOException IOCVUtils.sourceEncoding = sourceEncoding;IOCVUtils.targetEncoding = targetEncoding;changeEncoding(sourceFile, targetFile);或者package com.folkSeal.test;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class StreamGobbler extends Thread InputStream is; String type; StreamGobbler(InputStream is, String type) this.is = is; this.type = type; public void run() try InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line = null; while (line = br.readLine() != null) System.out.println(type + + line); catch (IOException ioe) ioe.printStackTrace(); package com.folkSeal.test;import java.io.File;/* * 用于文件夹下的java文件的编码自动转换,如gbk转utf-8 * * author Wen Fuqiang * company Fayhong Technology Co., Ltd. * date 2010-1-26 */public class EncodingConverter /* * main方法入口 * * param args * args0 给定需要转换的文件夹 args1 指定需要转换的编码,如utf-8等 */public static void main(String args) / if (args.length2)/ System.out.println(please input path of folder and encoding name);/ System.exit(1);/ / else/ ec.convertEncode(args0, args1);/ EncodingConverter ec = new EncodingConverter();/ 暂时用src_path替换args0,encoding_name替换arg1String src_path = E:WorkspacesfolkSealWebRootfileseal_templet_filexml22;String encoding_name = utf-8;ec.convertEncode(src_path, encoding_name);public void convertEncode(String sourceFloder, String encoding_name) File file = new File(sourceFloder);String files = file.list();for (String s : files) if (s.indexOf(.) = -1) / 表明这是个子目录,回归调用此函数convertEncode(file.getAbsolutePath() + + s, encoding_name); else if (s.endsWith(xml) / 只处理以Java结尾的文件doConvertEncode(file.getAbsolutePath() + + s, file.getAbsolutePath()+ + s, encoding_name);/* * 完成具体的编码转换工作 * * param inputFile * 输入文件 * param outputFile * 输出文件 * param encoding_name * 需要转成的编码格式 */public void doConvertEncode(String inputFile, String outputFile,String encoding_name) Runtime rt = Runtime.getRuntime();String cmd = native2ascii.exe, -reverse, -encoding,encoding_name, inputFile, outputFile ;System.out.println(Execing convert command for + inputFile + .);try Process proc = rt.exec(cmd);/ any error message?StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), ERROR);/ any output?StreamGobbler outp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年金属制品修理行业市场前景报告及未来五至十年产业链安全与韧性提升
- 【基础题】证券从业资格考试《金融市场基础知识》真题及答案详解
- 2026年ABF载板行业洞察报告及未来五至十年发展趋势报告
- 2026年半导体制造装备行业洞察报告及未来五至十年发展趋势报告
- 2026年防水标准考试题库及答案
- 2026年新版煤矿安管证考试题及答案
- 2026年美式咖啡机行业洞察报告及未来五至十年发展趋势报告
- 2026年纤维素纤维原料及纤维制造行业产业趋势报告及未来五至十年市场空间与盈利前景
- 2026年搪瓷卫生洁具制造行业产业洞察报告及未来五至十年需求升级与结构演变
- 技术研发合作成果分配协议合同二篇
- 2026陕西省农村信用社联合社金融科技岗校园招聘20人笔试模拟试题及答案解析
- 后浇带止水钢板及膨胀止水条方案
- 2026年广东省普通高中第一次学业水平合格性考试地理试卷
- 2026年中国魔芋种植行业规模现状及十三五投资规划研究报告
- 2026年江苏省高考生物试卷真题(含答案)
- 2026年技能人才评价督导员考试卷及答案
- 烈士褒扬条例修订解读
- 光伏储能电站运行维护手册
- 数字电子技术讲解杨志忠版
- 2026-2027学年四年级上册数学单元全真模拟培优卷(人教版)第4单元 加法模型和乘法模型
- 国网2026变配电运行值班员-中级工考试题库
评论
0/150
提交评论