Because UTF8 is set in eclipse, all the code written by others is garbled.
No gbk can be found in eclipse, so a tool conversion is written.
Directory iteration is 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;
/**
* Put the gbk code under srcDir and set transcoding to destDir. A directory under srcDir is not supported for the time being and must be added.
* @ 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. Identify as a directory
If (! SrcDir. isDirectory ()){
Return;
}
// 2. traverse all directories
File [] fs = srcDir. listFiles ();
// Create the target directory
If (! DestDir. exists ()){
DestDir. mkdirs ();
}
Try {
New UTF8Parser (). parse (fs );
} Catch (IOException e ){
E. printStackTrace ();
}
}
/**
* The Directory is iterated and the file is reencoded.
* @ 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 and write objects
PrintWriter ps = new PrintWriter (new OutputStreamWriter (new FileOutputStream (destFile, false), "utf8 "));
BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (file), "gbk "));
// Read/write actions
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 ();
}
}