import java.io.*;
//---------------------------------
//
class HTMLFilter implements FilenameFilter {
public boolean accept(File directory, String name) {
if (name.endsWith(".html")) return true;
if (name.endsWith(".htm")) return true;
if (name.endsWith(".HTML")) return true;
if (name.endsWith(".HTM")) return true;
return false;
}
}
class BatchEncoder {
String oldFile = null;
public BatchEncoder(String oldFile) {
this.oldFile = oldFile;
File f = new File(oldFile);
tree(f);
}
public void tree(File f) {
File[] childs = f.listFiles(new HTMLFilter());
if (childs == null) {
return;
}
for (int i = 0; i < childs.length; i++) {
if (childs[i].isDirectory()) {
tree(childs[i]);
} else if (childs[i].isFile()) {
parse(childs[i]);
}
}
}
public void parse(File f) {
BufferedReader br = null;
BufferedWriter bw = null;
String UTF = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />";
String s = "";
try {
InputStreamReader isr = new InputStreamReader(
new FileInputStream(f));
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream("D:\\BatchEncoder_bak\\" + f.getName()),
"UTF-8");
br = new BufferedReader(isr);
bw = new BufferedWriter(osw);
if (!(isr.getEncoding().equals("UTF8"))) {
while ((s = br.readLine()) != null) {
if (!(s.matches("<meta.*charset=UTF-8.*>$"))) {
s = s.replaceAll("<meta.*>$", UTF);
}
bw.write(s);
bw.newLine();
}
bw.flush();
testEncoding(f, osw);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void testEncoding(File f, OutputStreamWriter osw) {
if(f.isFile()) {
if (osw.getEncoding().equals("UTF8")) {
System.out.println(f.getName()
+ " --> Encoding resounding success! current Encoding is: "
+ osw.getEncoding());
} else {
System.out.println(f.getName()
+ " --> Encoding failure! current Encoding is:"
+ osw.getEncoding());
}
}
}
}
// ------------------------------------------------------------
// Main Method
public class TestBatchEncoder {
public static void main(String[] args) {
BatchEncoder be = new BatchEncoder("D:\\BatchEncoder");
}
}