Skip to content

Instantly share code, notes, and snippets.

@commana
Created January 10, 2009 13:38
Show Gist options
  • Save commana/45456 to your computer and use it in GitHub Desktop.
Save commana/45456 to your computer and use it in GitHub Desktop.
UTF-8 File Conversion for Windows
// Code by Anton Afanasjew
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
public class FileListExample {
public static final String CMD = "run.cmd";
public static final String ROOT = "files";
static String line = "";
static InputStream stderr = null;
static InputStream stdout = null;
static int COUNT = 0;
public static void main(String[] args) throws Exception {
treeWalk(new File(ROOT));
System.out.println(COUNT);
}
static void treeWalk(File src) throws Exception {
if (src.isDirectory()) {
File[] children = src.listFiles();
for (File child : children) {
treeWalk(child);
}
} else {
if (isRelevantFile(src)) {
COUNT++;
process(src);
}
}
}
static void process(File f) throws Exception {
Process process = Runtime.getRuntime().exec(CMD + " " + "\"" + f.getAbsolutePath() + "\"");
stderr = process.getErrorStream();
stdout = process.getInputStream();
BufferedReader brCleanUp = new BufferedReader(new InputStreamReader(stdout));
while ((line = brCleanUp.readLine()) != null) {
System.out.println(line);
}
brCleanUp.close();
brCleanUp = new BufferedReader(new InputStreamReader(stderr));
while ((line = brCleanUp.readLine()) != null) {
System.out.println(line);
}
brCleanUp.close();
process.waitFor();
}
static boolean isRelevantFile(File f) {
return
f.getName().endsWith(".php") ||
f.getName().endsWith(".html")||
f.getName().endsWith(".txt") ||
f.getName().endsWith(".sql") ||
f.getName().endsWith(".mkf") ||
f.getName().endsWith(".xml") ||
f.getName().endsWith(".css") ||
f.getName().endsWith(".js");
}
}
iconv -f cp1252 -t utf-8 %1 > %1.oldfiletodelete
copy %1.oldfiletodelete %1
del %1.oldfiletodelete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment