This article mainly introduces the method examples of converting GBK projects to uft8 using Java. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
This article introduces how to use java to convert GBK projects to uft8 and share it with everyone. The details are as follows:
The default encoding under windows is GBK and gb2312. How to convert gbk's java project to utf8? If you modify the project encoding directly, the Chinese characters in the java files inside will actually be garbled. I wrote a program for batch conversion of java projects for fun.
Why transcode?
Some old projects, or friends’ projects, didn’t notice before that they were not UTF8 on Windows, and if you need to read comments or something, you can’t change the encoding attributes one file at a time.
The trial scope of this program
gbk code or gb2312 project can be converted
The idea of coding conversion
Originally I wanted to make a universal program that can automatically detect encoding and convert automatically. However, due to the inaccurate judgment of the encoding type, conversion to GBK was made.
Specify gbk encoding to read the file stream, load it into memory, and convert it to String type content
Convert String content to utf8 String
Write String content to file
Core code:
public class TransferProject{
public static void transferFile(String pathName,intdepth)throwsException{
File dirFile = new File(pathName);
if (!isValidFile(dirFile)) return;
//获取此目录下的所有文件名与目录名
String[] fileList = dirFile.list();
int currentDepth = depth + 1;
for (int i = 0; i < fileList.length; i++) {
String string = fileList[i];
File file = new File(dirFile.getPath(), string);
String name = file.getName();
//如果是一个目录,搜索深度depth++,输出目录名后,进行递归
if (file.isDirectory()) {
//递归
transferFile(file.getCanonicalPath(), currentDepth);
} else {
if (name.contains(".java") || name.contains(".properties") || name.contains(".xml")) {
readAndWrite(file);
System.out.println(name + " has converted to utf8 ");
}
}
}
}
private static boolean isValidFile(File dirFile)throwsIOException{
if (dirFile.exists()) {
System.out.println("file exist");
return true;
}
if (dirFile.isDirectory()) {
if (dirFile.isFile()) {
System.out.println(dirFile.getCanonicalFile());
}
return true;
}
return false;
}
private static void readAndWrite(File file)throwsException{
String content = FileUtils.readFileByEncode(file.getPath(), "GBK");
FileUtils.writeByBufferedReader(file.getPath(), new String(content.getBytes("UTF-8"), "UTF-8"));
}
public static void main(String[] args)throwsException{
//程序入口,制定src的path
String path = "/Users/mac/Downloads/unit06_jdbc/src";
transferFile(path, 1);
}
}
public class FileUtils{
public static void writeByBufferedReader(String path, String content){
try {
File file = new File(path);
file.delete();
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public staticStringreadFileByEncode(String path, String chatSet)throwsException{
InputStream input = new FileInputStream(path);
InputStreamReader in = new InputStreamReader(input, chatSet);
BufferedReader reader = new BufferedReader(in);
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
while (line != null) {
sb.append(line);
sb.append("\r\n");
line = reader.readLine();
}
return sb.toString();
}
}The above is the detailed content of How to use Java to convert GBK project to uft8 method sharing. For more information, please follow other related articles on the PHP Chinese website!
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PMStart Spring using IntelliJIDEAUltimate version...
How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PMWhen using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...
Java BigDecimal operation: How to accurately control the accuracy of calculation results?Apr 19, 2025 pm 11:39 PMJava...
How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PMHow does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...
How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PMConversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...
How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PMSolutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...
E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PMDetailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PMHow to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment






