import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* @author peter
*
*/
public class EncodingConverter {
String fromCharset;
String toCharset;
String baseDir;
String encodedDir;
EncodingConverter(String baseDir, String fromCharset, String toCharset) {
this.fromCharset = fromCharset;
this.toCharset = toCharset;
this.baseDir = baseDir;
this.encodedDir = baseDir + File.separator + "encoded";
File encoded = new File(encodedDir);
encoded.mkdir();
}
public boolean converts(File file) {
if (!file.exists())
return false;
if (file.isDirectory() && !file.getName().startsWith(".")) {
String as[] = file.list();
if (as != null && as.length > 0) {
for (int i = 0; i < as.length; i++)
if (!converts(new File(file, as[i])))
return false;
}
}
if (!file.getName().startsWith(".") && file.isFile()) {
String toPath = encodedDir + file.getParent().substring(baseDir.length());
System.out.println(toPath);
File dir = new File(toPath);
dir.mkdirs();
return convert(file, new File(toPath + File.separator + file.getName()));
} else
return true;
}
public boolean convert(File fromFile, File toFile) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile), fromCharset));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), toCharset));
String s;
while ((s = in.readLine()) != null) {
out.write(s);
out.newLine();
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static void main(String[] args) {
EncodingConverter converter = new EncodingConverter("D:\\work\\src", "MS949", "UTF-8");
converter.converts(new File(converter.baseDir));
}
}
댓글 없음:
댓글 쓰기