Eclipse spent a period of time, are used by the GBK code, suddenly want to standardize, obsessive-compulsive disorder, so will be modified in window->preference->appearances->workspace text file Encoding for UFT-8, a click to confirm, Oh,my god! Chinese are garbled, the first thought is to use Notepad to save a file as a UTF-8 format , exhausted, found that the project is too large, give up, moving code , Here is the code that can be implemented, support batch processing (that is, all the code within a folder), do not support folders under the situation! The code is as follows:
PackageCom.tfj.demo;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;/*** @function convert batch GBK garbled files to UTF8 * put GBK code under Srcdir, transcoding Destdir, not supported under Srcdir directory, Srcdir directory and Destdir directory can not be the same *
@authorjacksile e-mail:[email Protected] * @date January 16, 2016 PM 3:02:07*/ Public classUtf8parser {StaticFile Srcdir =NewFile ("E:\\eclipse2\\studyall\\src\\com\\tfj\\algorithm");//GBK Format folder for transcoding StaticFile DestDir =NewFile ("E:\\eclipse\\studyall\\src\\com\\tfj\\algorithm");//transcoding to UTF8 target folder Public Static voidMain (string[] args) {//1. Judging is the directory if(!srcdir.isdirectory ()) { return; } //2. Traverse All Directoriesfile[] fs =Srcdir.listfiles (); //Create target directory if(!destdir.exists ()) {Destdir.mkdirs (); } Try { NewUtf8parser (). Parse (FS); } Catch(IOException e) {e.printstacktrace (); } } /*** The directory iterates over the file and re-encodes it.*/ Private voidParse (file[] FS)throwsIOException { for(File file:fs) {if(!file.isdirectory ()) {File DestFile=NewFile (DestDir, File.getname ()); Parse2utf_8 (file, destfile); } Else{Parse (file.listfiles ()); } } } /** */ Private voidParse2utf_8 (file file, file DestFile)throwsIOException {stringbuffer msg=NewStringBuffer (); //Read and Write ObjectsPrintWriter PS =NewPrintWriter (NewOutputStreamWriter (NewFileOutputStream (DestFile,false), "UTF8")); BufferedReader BR=NewBufferedReader (NewInputStreamReader (NewFileInputStream (file),"GBK")); //Read and write ActionsString line =Br.readline (); while(Line! =NULL) {msg.append (line). Append ("\ r \ n"); Line=Br.readline (); } ps.write (Msg.tostring ()); Br.close (); Ps.flush (); Ps.close (); }}
Modify the folder name in the code, run, copy all files from the converted folder to the original folder, modify the folder name in the code ... Back and forth these three steps, found my folder too much, so also will be tired of half-dead, so can only switch to conversion tools , online Search: GBK to Utf-8 software, a lot of tools, many are support batch processing, open tools, select a folder ( this can be arbitrary, There is no limit, the above code also restricts the folder can not exist in the folder, easy to convert, this, the coding problem is solved completely. the only regret is that such a large batch processing will cause the original Utf-8 file error, so it is recommended that when the batch processing folder to maximize the situation to avoid the existence of files that are already utf-8, otherwise properly shrink the folder contains the scope.
Eclipse modified encoding after garbled resolution