



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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游戏委托开发合同范本
- 华与华合同范例
- 跨境运输问题应急预案
- 秋季流行服饰与衣料的准备-生产计划讲义教材
- 七年级上册地理人教版知识清单
- HDPE塑钢缠绕排水管施工方案
- 二十案例示轮回
- 蒋诗萌小品《谁杀死了周日》台词完整版
- 农业综合行政执法大比武试题库(试题及答案)
- (新版)婴幼儿发展引导员(初级)技能鉴定理论试题库(含答案)
- 颅高压危象课件
- 《椎管内肿瘤》课件
评论
0/150
提交评论