In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.