Java converts batches of gbk garbled files to UTF8

Source: Internet
Author: User

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


}





}

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.