Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand Java character coding

Shulou Source: shulou.com Published: 2022-06-02 19:33:23 09月10日 Update

This article mainly introduces "how to understand Java character coding". In daily operation, I believe many people have doubts about how to understand Java character coding. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Java character coding". Next, please follow the editor to study!

Preface

In the computer, any text exists in the specified coding mode, and the most common ones in the development of Java programs are ISO8859-1, GBK/GB2312, Unicode, UTF coding.

Common encodings in Java

(1) ISO8859-1: belongs to a single-byte encoding and can only represent a range of characters of 0x255 at most.

(2) GBK/GB2312: Chinese national standard code, used to represent Chinese characters, belongs to double-byte coding. GBK can represent simplified Chinese and traditional Chinese, while GB2312 can only represent simplified Chinese. GBK is compatible with GB2312.

(3) Unicode: is a coding specification, is designed to solve the global universal character coding. UTF-8 and UTF-16 are an implementation of this specification, and this encoding is not compatible with ISO8859-1 coding. This code is used internally in Java.

(4) UTF:UTF coding is compatible with ISO8859-1 encoding, and can also be used to represent all language characters, but UTF coding is a variable-length code, and the length of each character varies from 1 to 6 bytes. Generally, this coding is used in Chinese web pages to save space.

If the character encoding is not handled well in the program, garbled problems may occur. For example, now the default encoding of this machine is GBK, but if ISO8859-1 encoding is used in the program, there will be the problem of garbled characters. Just like people from two different countries communicate in their mother tongue, they cannot communicate with each other if they speak different languages. So in order to avoid garbled code, the program code should be consistent with the local default code.

Public static Properties getProperty () / / you can view the default encoding used by the current system

Code example:

Public static void main (String [] args) {

/ / get the current system code

System.out.println ("system default Code:" + System.getProperty ("file.encoding"))

}

Coding error case:

Public class Test {

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

File f = new File ("D:" + File.separator + "test.txt")

/ / instantiate the output stream

OutputStream out = new FileOutputStream (f)

/ / specify ISO8859-1 encoding

Byte b [] = "Hello, C language Chinese website!" .getBytes ("ISO8859-1")

/ / Save the transcoded data

Out.write (b)

/ / close the output stream

Out.close ()

}

}

The running results are as follows:

Thus it can be seen that there is garbled code when saving due to the inconsistent coding.

At this point, the study of "how to understand Java character coding" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Coding character Chinese garbled program learning byte system language problem different consistent common more help output practical next bad two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple vpn Xiaomi Shulou Tech Info Huawei