Java批量转换文件编码
来源:互联网 发布:求解无约束优化问题 编辑:程序博客网 时间:2024/05/17 08:52
转载:http://hi.baidu.com/zhjunxue/blog/item/5f94ca8af9da00d4fc1f105f.html
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 (dataFiles[i].isDirectory()) {
getFileList(dataFiles[i].getCanonicalPath());
} else {
//文件过滤,如无需此项注释此处即可
if (dataFiles[i].isFile()
&& (dataFiles[i].getName().endsWith(".jsp")
|| dataFiles[i].getName().endsWith(".html") || dataFiles[i]
.getName().endsWith(".htm"))
|| dataFiles[i].getName().endsWith(".js")
|| dataFiles[i].getName().endsWith(".java")) {
writeFile(dataFiles[i].getAbsolutePath(),
dataFiles[i].getAbsolutePath()
.substring(
2,
dataFiles[i].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 byte[length];
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 byte[length];
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.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return isSucc;
}
/**
* @param args
*/
public static void main(String[] args) {
FileList fl = new FileList();
fl.getFileList("D:\\speciality");
}
}
- Java批量转换文件编码
- 批量转换文件编码
- 批量转换文件编码
- 批量文件编码转换
- php文件编码批量转换
- 批量转换文件编码_php
- python批量转换文件编码
- linux批量转换文件编码
- Linux批量转换文件编码
- shell批量文件编码转换
- python批量转换文件编码
- Eclipse 批量转换文件编码
- notepad++批量转换文件编码
- 文件乱码,批量转换文件编码
- 批量转换Uncode编码的文件
- 利用codecs模块批量转换文件编码
- Python3 批量转换文件编码 Encoding
- linux下批量转换文件编码
- java文件编码格式转换
- js取得各种尺寸
- Apache限制浏览器类型
- CSS中position属性详解以及定位的说明——实验1
- poj1260 dp
- Java批量转换文件编码
- 如何在.net应用中发现和避免内存和资源泄露
- 2011年腾讯实习生应聘(软件开发>Web前端>flash方向)笔试面试经历
- 腾讯校园招聘面试的秘密
- 字符编解码:jdk1.5.x与1.6.x,Unicode字节序列存在的差别
- ImportError: No module named setuptools 解决方案
- SQL Server性能优化的一些技巧
- CSS中position属性详解以及定位的说明——实验2
- C/C++面试题集(最全的C/C++试题集和答案)(续)