



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java批量转换文件编码2 package file;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;/* 可用来批量转换代码的编码.并保持文件段落及缩进* author Justin*/public class FileList /获取某个目录下面的所有文件public File getFileDir(String dir) File dataDir = new File(dir); File dataFiles = dataDir.listFiles(); return dataFiles;public boolean getFileList(String dir) boolean isSucc = false; File dataDir = new File(dir); File dataFiles = dataDir.listFiles(); try for (int i = 0; i dataFiles.length; i+) /如果是目录则递归调用此方法.继续检索 if (dataFilesi.isDirectory() getFileList(dataFilesi.getCanonicalPath(); else /文件过滤,如无需此项注释此处即可 if (dataFilesi.isFile() & (dataFilesi.getName().endsWith(.jsp) | dataFilesi.getName().endsWith(.html) | dataFilesi .getName().endsWith(.htm) | dataFilesi.getName().endsWith(.js) | dataFilesi.getName().endsWith(.java) writeFile(dataFilesi.getAbsolutePath(), dataFilesi.getAbsolutePath() .substring( 2, dataFilesi.getAbsolutePath() .length(); catch (IOException e) e.printStackTrace(); return isSucc;/以默认编码读取文件内容.并以字条串返回private String readFile(String templet) String templetContent = ; try FileInputStream fileinputstream = new FileInputStream(templet); int length = fileinputstream.available(); byte bytes = new bytelength; fileinputstream.read(bytes); fileinputstream.close(); templetContent = new String(bytes); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); return templetContent;/ 以UTF-8读取文件内容.并以字条串返回private String readFileUTF_8(String templet) String templetContent = ; try FileInputStream fileinputstream = new FileInputStream(templet); int length = fileinputstream.available(); byte bytes = new bytelength; fileinputstream.read(bytes); fileinputstream.close(); templetContent = new String(bytes, UTF-8); catch (FileNotFoundException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); return templetContent;/在新文件夹建立相同的目录结构private void makeDir(String path) File fileStr = new File(path); File file = new File(fileStr.getParent(); if (!file.exists() file.mkdirs(); public boolean writeFile(String realPath, String fileName) boolean isSucc = false; String templet = realPath; String newFile = D:/NewFiles/ + fileName.substring(1, fileName.length(); makeDir(newFile); String templateContent = ; /读取文件内容.如需以UTF-8读取.换成readFileUTF_8(filePath)即可 templateContent = readFile(templet); /如果是网页或其它注明了编码的文件.用此方法替换 templateContent = templateContent.replaceAll(gb2312, UTF-8); templateContent = templateContent.replaceAll(gbk, UTF-8); /* templateContent = templateContent.replaceAll(UTF-8, GBK); templateContent = templateContent.replaceAll(utf-8, GBK);*/ /templateContent = templateContent.replaceAll(GB2312, UTF-8); /templateContent = templateContent.replaceAll(gbk, UTF-8); /System.out.println(输出: + templateContent); isSucc = writeNewFile(newFile, templateContent); System.out.println(输出: + newFile); return isSucc;/* 以UTF-8形式写新文件* param newFile* param templetContent* return*/private boolean writeNewFile(String newFile, String templetContent) boolean isSucc = false; try FileOutputStream fout = new FileOutputStream(newFile); OutputStreamWriter out = new OutputStreamWriter( new BufferedOutputStream(fout), UTF-8); out.write(templetContent); out.close(); fout.close(); isSucc = true; catch (FileNotFoundException e) e.printStackTrace(); catch (UnsupportedEncodingException e) e.pr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025公务员返聘面试题及答案
- 2025公务员法学面试题及答案
- 2025年茶叶品牌定制化产品生产与销售合作框架协议
- 2025年跨境电商供应链金融质押贷款合同模板
- 2025年度冷链物流运输服务经营权移交协议
- 2025年重点工程现场安保及临时劳动力派遣合同
- 2025年度环保瓷砖采购与绿色施工技术支持协议
- 2025年金融科技赋能的农村小额贷款服务协议
- Unit2重点词汇与短语讲解课件人教版八年级英语上册
- 2025年金融领域专业人才猎头服务协议(含风险控制与合规要求)
- 肾上腺疾病外科治疗
- 第9章探放水钻机及相关设备的安全使用.
- (高清正版)JJF 1908-2021 双金属温度计校准规范
- 交通部农村公路建设标准指导意见
- 卫浴店面管理
- 清表施工方案4常用
- 纯化水系统再验证方案E
- 5立方米液化石油气储罐课程设计说明书
- 临床技术操作规范_骨科学分册资料全
- 华为内部虚拟股管理暂行条例
- 集装箱箱主代码
评论
0/150
提交评论