文本替换java实现
批量将html中gbk字符编码替换为UTF8:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class ContextReplace {
private static String srcStr = "charset=gb2312";
private static String desStr = "charset=utf-8";
private static String srcDir = "f:\\test";
private static FileFilter filter = new FileFilter() {
public boolean accept(File pathname) {
// 只处理:目录 或是 .html文件
if (pathname.isDirectory()
|| (pathname.isFile() && pathname.getName().endsWith(
".html"))) {
return true;
} else {
return false;
}
}
};
public static void readDir(File file) {
// 以过滤器作为参数
File[] files = file.listFiles(filter);
for (File subFile : files) {
// 处理目录
if (subFile.isDirectory()) {
readDir(subFile);
}
// 处理文件
else {
System.err.println("源文件:\t" + subFile.getAbsolutePath());
System.err.println("---------------------------");
try {
replace(subFile.getAbsolutePath(), srcStr, desStr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void replace(String infilename, String from, String to)
throws IOException, UnsupportedEncodingException {
File infile = new File(infilename);
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(infile), "utf-8"));
File outfile = new File(infile + ".tmp");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(outfile), "utf-8")));
String reading;
while ((reading = in.readLine()) != null) {
out.println(reading.replaceAll(from, to));
}
out.close();
in.close();
infile.delete();
outfile.renameTo(infile);
}
public static void main(String[] args) {
File srcFile = new File(srcDir);
readDir(srcFile);
srcFile = null;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/poi987/archive/2010/04/13/5482486.aspx