Bulk convert Java source code file encoding from GBK to UTF-8

Source: Internet
Author: User

Method 1: (procedural law)
Problem scenario: because the old project adopts the GBK encoding format, and the new project adopts the UTF-8 encoding format, if the Java source code is copied directly to all the Chinese information in eclipse there are garbled characters. There are many ways to convert the encoding format of a text file from GBK to UTF-8,
Earlier, I used to write programs by myself, read data from text files using GBK encoding, convert it to the string type, and then re-write the text file through UTF-8 encoding for transcoding, now often use Apache common components, use commons-io.jar to implement the file
Read and Write the Code as follows:

// GBK encoding format source code path
String srcdirpath = "D: \ Dev \ workspace \ masdev \ mas \ SRC ";
// Convert to UTF-8 encoding format source code path
String utf8dirpath = "d :\\ utf8 \ SRC ";

// Obtain all java files
Collection <File> javagbkfilecol = fileutils. listfiles (new file (srcdirpath), new string [] {"Java"}, true );

For (File javagbkfile: javagbkfilecol ){
// Utf8 file path
String utf8filepath = utf8dirpath + javagbkfile. getabsolutepath (). substring (srcdirpath. Length ());
// Use GBK to read data and then write data with UTF-8
Fileutils. writelines (new file (utf8filepath), "UTF-8", fileutils. readlines (javagbkfile, "GBK "));
}

A few lines of code, you can batch GBK format of java files into UTF-8 format. Basically, this method can be used for encoding and conversion of all text files.

Reference: http://my.oschina.net/binny/blog/62959

Method 2: (eclipse file transcoding plug-in method)
, Baidu Network Disk: http://pan.baidu.com/share/link? Consumer id = 23599 & UK = 3573694317

 

Step1. copy the plug-in COM. lifesting. tool. encoding_1.0.0.jar to the eclipse/Plugins directory and restart eclipse.

Step2. select a project, right-click Properties, and click convert setting to set transcoding rules, as shown in:

(Figure 2 configure project transcoding Rules)
This table has four columns: file suffix, current file encoding, converted encoding, and whether to convert the file content. If you convert the file content, it corresponds to the second situation mentioned above, the first case is that the file content is not converted. Than 2 of the transcoding rules is to project JSP files, no matter what the original encoding, unified into UTF-8 encoding, and at the same time to convert the file content. You can click Add/Remove on the right to add or delete rules. Click Apply to save the settings.

Step3. Transcoding
On the project with transcoding rules set, right-click the project, select several folders or several files (of course one can), and then click lifesting tools-> set encoding. The plugin completes transcoding. If you select a project, the entire project will be converted; if you select a folder, all the files under the folder that meet the rules will be converted; if you select a file, only the files will be converted.

(Figure 3 transcoding)
In addition to this method, we can also use the file search function of eclipse to transcode the searched files, such:

(Figure 4 transcoding in search view)
As shown in, we found the JSP file of the entire workspace, and then click the green e small icon, the plug-in will transcode all the files that meet the results, the conversion rule of each file depends on the transcoding rule settings of the project to which it belongs.

 

NOTE: If batch transcoding is performed, it may take some time. Some code conversion problems still exist .......

References: http://blog.csdn.net/hdfyq/article/details/5738759

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.