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批量转换文件编码,责任编辑:宏鹏,来源:武汉北大青鸟鲁广校区栏目,于2013-01-24 09:58:30发布于北大青鸟鲁广校区。可用来批量转换代码的编码.并保持文件段落及缩进,如果是目录则递归调用此方法.继续检索,以默认编码读取文件内容.并以字条串返回。
预约将免费领取7天课程体验卡
只为您方便就学
专业老师24小时1对1学习指导
定制专属于你的专属学习方案
微信号:17740513250
微信号:17740513250