public class TransferProject {
public static void transferFile(String pathName, int depth) throws Exception {
File dirFile = new File(pathName);
if (!isValidFile(dirFile)) return;
String[] fileList = dirFile.list();
int currentDepth = depth + 1;
for (int i = 0; i < fileList.length; i++) {
String string = fileList[i];
File file = new File(dirFile.getPath(), string);
String name = file.getName();
if (file.isDirectory()) {
transferFile(file.getCanonicalPath(), currentDepth);
} else {
if (name.contains(".java") || name.contains(".properties") || name.contains(".xml")) {
readAndWrite(file);
System.out.println(name + " has converted to utf8 ");
}
}
}
}
private static boolean isValidFile(File dirFile) throws IOException {
if (dirFile.exists()) {
System.out.println("file exist");
return true;
}
if (dirFile.isDirectory()) {
if (dirFile.isFile()) {
System.out.println(dirFile.getCanonicalFile());
}
return true;
}
return false;
}
private static void readAndWrite(File file) throws Exception {
String content = FileUtils.readFileByEncode(file.getPath(), "GBK");
FileUtils.writeByBufferedReader(file.getPath(), new String(content.getBytes("UTF-8"), "UTF-8"));
}
public static void main(String[] args) throws Exception {
String path = "/Users/mac/Downloads/unit06_jdbc/src";
transferFile(path, 1);
}
}