How to use Java code to convert a GBK encoded format project to a UTF-8 encoded format

Source: Internet
Author: User

Alas, finally set up-the project when the coding method for GBK, but with the continuous development of the final decision will be the code of the project to UTF-8, so that all the Java files containing Chinese have appeared garbled, hundreds of files ah, a change that still not exhausted, moved the brain, Write a piece of Java code, instant fix, the code is as follows:

package Com.ghj.packageofclient;import Java.io.file;import Java.util.collection;import org.apache.commons.io.FileUtils;/* * * Convert GBK encoded files to UTF-8 encoded format * * @author Gao Yingjie */public class GBKToUTF8 {public static void main (string[] args) throws Ex ception {String gbkdirpath = "D:\\SVNANDROID\\MEGREZMOBILE\\MEGREZLIB\\SRC";//GBK encoded format source file path String Utf8dirpath  = "D:\\UTF8\\SRC";//Convert to UTF-8 encoded format source file save path @SuppressWarnings ("unchecked") collection<file> gbkfilelist =    Fileutils.listfiles (new file (Gbkdirpath), new string[]{"Java"}, True);//Get all Java files for (file gbkfile:gbkfilelist) { String Utf8filepath = Utf8dirpath + gbkfile.getabsolutepath (). substring (Gbkdirpath.length ());//utf-8 encoded format File save path Fileu Tils.writelines (new file (Utf8filepath), "UTF-8", Fileutils.readlines (Gbkfile, "GBK"));//Use GBK encoded format to read files, Then write the data in UTF-8 encoded format}}} 
This project relies on a jar package from Apache, so it is recommended to download the Java project directly.

"0-minute download demo"

How to use Java code to convert a GBK encoded format project to a UTF-8 encoded format

Related Article

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.