Because of their eclipse settings UTF8, import other people write code, are all garbled.
GBK was not found under Eclipse, so a tool was written to convert it.
Directory iterations are not supported.
package com.iteye.niewj.util;
import Java.io.BufferedReader;
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 Java.io.PrintWriter;
/**
* will GBK code under the Srcdir, transcoding destdir below, temporarily does not support the directory under Srcdir, there is a need to add
* @author NIEWJ
* @2012-6-1
*/
public class Utf8parser {
/**
* @author NIEWJ
* @since 2012-6-1
*/
static File Srcdir = new File ("d:/prj/java/niewj_oy/eclipse_swt_src/src/swt/");
Static File destdir= New file ("d:/prj/java/niewj_oy/eclipse_swt_src/src/swt_cp/");
public static void Main (string[] args) {
//1. Judgment is directory
if (!srcdir.isdirectory ()) {
return;
}
//2. Traverse All Directories
file[] fs = Srcdir.listfiles ();
//Create target directory
if (!destdir.exists ()) {
Destdir.mkdirs ();
}
try {
new Utf8parser (). Parse (FS);
} catch (IOException e) {
E.printstacktrace ();
}
}
/**
* directory is iterated traversal; file is encoded
* @author NIEWJ
* @since 2012-6-1
*/
private void Parse (file[] fs) throws IOException {
for (File file:fs) {
if (!file.isdirectory ()) {
File DestFile = new file (Destdir,file.getname ());
parse2utf_8 (File,destfile);
}else{
Parse (File.listfiles ());
}
}
}
/**
* @author NIEWJ
* @since 2012-6-1
*/
private void Parse2utf_8 (File file,file destfile) throws IOException {
stringbuffer msg = new StringBuffer ();
//Read/write Object
PrintWriter PS = new PrintWriter (new OutputStreamWriter (New FileOutputStream (destfile,false), "UTF8"));
bufferedreader br = new BufferedReader (new InputStreamReader (new FileInputStream (file), "GBK"));
//Read/write Action
String line = Br.readline ();
while (line!=null) {
Msg.append (line). Append ("RN");
line = Br.readline ();
}
Ps.write (Msg.tostring ());
Br.close ();
Ps.flush ();
Ps.close ();
}
}