package ch03;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.junit.Test;
public class TestEncodeConverter {
@Test
public void testEncodeConverter() {
String sourcePath = "D:\\code\\szgr\\ruledrools_bak\\src";
String destPath = "d:\\converter\\src";
File file = new File(sourcePath);
convert(file, sourcePath, destPath);
System.out.println("----------------------------------------------------------------------------------------------------------------------");
sourcePath = "D:\\code\\szgr\\ruledrools_bak\\test";
destPath = "d:\\converter\\test";
file = new File(sourcePath);
convert(file, sourcePath, destPath);
}
private void createDirectory(File file){
if(file.exists()){
System.out.println("true");
return;
}
File parentFile = file.getParentFile();
if(!parentFile.exists()){
createDirectory(parentFile);
file.mkdir();
}else{
file.mkdir();
}
}
// D:\code\szgr\ruledrools_bak\src\com\szgr\ruledrools\bean\converter\basic\StringConverter.java
private void convert(File file, String sourcePath, String destPath) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
convert(f, sourcePath, destPath);
}
// System.out.println(file.getAbsolutePath());
} else {
if (file.getName().endsWith(".java")
|| file.getName().endsWith(".properties") || file.getName().endsWith(".xml")
|| file.getName().endsWith(".drl")) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis, "GBK");
BufferedReader br = new BufferedReader(reader);
String endPath = file.getAbsolutePath().substring(
sourcePath.length());
endPath = endPath.startsWith("\\") ? endPath : "\\"+endPath;
String newFileName = destPath
+ endPath;
File f = new File(newFileName);
File parentFile = f.getParentFile();
System.out.println(newFileName+"="+parentFile);
createDirectory(parentFile);
if (!f.exists()) {
f.createNewFile();
}
fos = new FileOutputStream(f);
OutputStreamWriter writer = new
OutputStreamWriter(fos,"utf-8");
BufferedWriter bw = new BufferedWriter(writer);
String line = null;
while((line = br.readLine()) != null){
bw.write(line+"\r\n");
}
bw.flush();
fos.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} } }