Java批量转换编码2.doc

上传人:re****.1 文档编号:543759106 上传时间:2022-12-01 格式:DOC 页数:4 大小:37KB
返回 下载 相关 举报
Java批量转换编码2.doc_第1页
第1页 / 共4页
Java批量转换编码2.doc_第2页
第2页 / 共4页
Java批量转换编码2.doc_第3页
第3页 / 共4页
Java批量转换编码2.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《Java批量转换编码2.doc》由会员分享,可在线阅读,更多相关《Java批量转换编码2.doc(4页珍藏版)》请在金锄头文库上搜索。

1、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;/* 可用来批量转换代码

2、的编码.并保持文件段落及缩进* 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 = dat

3、aDir.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

4、().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;/以默认编码读取文件内容.并以字条串返回priva

5、te 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 (Fil

6、eNotFoundException 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.av

7、ailable(); 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 pa

8、th) 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(); makeDi

9、r(newFile); String templateContent = ; /读取文件内容.如需以UTF-8读取.换成readFileUTF_8(filePath)即可 templateContent = readFile(templet); /如果是网页或其它注明了编码的文件.用此方法替换 templateContent = templateContent.replaceAll(gb2312, UTF-8); templateContent = templateContent.replaceAll(gbk, UTF-8); /* templateContent = templateCont

10、ent.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)

11、; 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 OutputS

12、treamWriter( new BufferedOutputStream(fout), UTF-8); out.write(templetContent); out.close(); fout.close(); isSucc = true; catch (FileNotFoundException e) e.printStackTrace(); catch (UnsupportedEncodingException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); return isSucc;/* param args*/public static void main(String args) FileList fl = new FileList(); fl.getFileList(D:speciality);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号