Home >Java >JavaBase >How to convert Chinese garbled characters in Java

How to convert Chinese garbled characters in Java

coldplay.xixi
coldplay.xixiOriginal
2020-10-20 10:26:374341browse

Java Chinese garbled conversion method: first use the editor to write the java source file; then use [javac.exe] to compile the java file, and use JDK to write the compiled and saved information in the memory into the class file Medium; finally run the compiled class.

How to convert Chinese garbled characters in Java

Java Chinese garbled conversion method:

Use encoding conversion

java encoding conversion process

We always use a java class file to conduct the most direct interaction (input, output) with users. The text contained in these interactive contents may contain Chinese. Whether these Java classes interact with the database or the front-end page, their life cycle is always like this:

1. Programmers write program code through the editor on the operating system and in .java format In the operating system, these files are called source files.

2. Compile these source files through javac.exe in the JDK to form the .class class.

3. Run these classes directly or deploy them in a WEB container to get the output results.

These processes are observed from a macro perspective. It is definitely not possible to understand this. We need to truly understand how java is encoded and decoded:

The first step : When we use the editor to write java source files, the program file will use the operating system's default encoding format when saving (generally our Chinese operating system uses the GBK encoding format) to form a .javadocument. Java source files are saved in the file.encoding encoding format supported by the operating system by default. The following code can view the system's file.encoding parameter value.

System.out.println(System.getProperty("file.encoding"));

Second step: When we use javac.exe to compile our java file, JDK will first confirm its compilation parameter encoding to determine the source code character set. If we do not specify For this compilation parameter, JDK will first obtain the default file.encoding parameter of the operating system, and then JDK will convert the java source program we wrote from the file.encoding encoding format to the default UNICODE format within JAVA and put it into memory.

Step 3: JDK writes the compiled information above and stored in memory into the class file to form a .class file. At this time, the .class file is Unicode encoded, which means that whether the content in our common .class file is Chinese characters or English characters, they have been converted to Unicode encoding format.

In this step, the processing of JSP source files is a little different: the WEB container calls the JSP compiler. The JSP compiler will first check whether the JSP file has a file encoding format set. If it is not set, the JSP compiler The processor will call JDK to convert the JSP file into a temporary servlet class using the default encoding method, and then compile it into a .class file and save it in a temporary folder.

Step 4: Run the compiled class: There will be several situations here

1. Run directly on the console.

2. JSP/Servlet class.

3. Between java class and database.

The methods in each of these three situations will be different

Related free learning recommendations: Java basic tutorial

The above is the detailed content of How to convert Chinese garbled characters in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn