Java converts garbled gbk files in batches to utf8

Source: Internet
Author: User

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 ();
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.