Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert Java source files encoded as GBK into UTF-8 in batches

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to convert GBK encoded Java source files to UTF-8 in bulk. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Recently in a project, need to migrate a lot of source code from another previous project, but because another project uses GBK encoding format, and the new project uses UTF-8 encoding format, if directly copy Java source code to Eclipse, all the Chinese information appears garbled.

There are many ways to convert the encoding format of text files from GBK to UTF-8. Earlier, I wrote my own program to read data from text files with GBK encoding, convert to String type, and then rewrite text files through UTF-8 encoding to achieve transcoding. Now I often use Apache Common components to read and write files with commons-io.jar. The code is as follows:

public static void main(String[] args) throws Exception {

//GBK encoding format source code path

String srcDirPath = "D:\\dev\\GBK\\src";

//Convert to UTF-8 encoding source code path

String utf8DirPath = "D:\\UTF8\\src";

//get all java files

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

//Read data with GBK, then write data with UTF-8

FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));

}

}

A few simple lines of code, you can batch GBK format java files into UTF-8 format. Basically all text files can be transcoded in this way.

Thank you for reading! About "how to code GBK Java source files into UTF-8 batch" This article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report