In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "what are the coding conversion methods in java". In the daily operation, I believe that many people have doubts about the coding conversion methods in java. 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 "what are the coding conversion methods in java?" Next, please follow the editor to study!
The principle of transcoding 1. Why do you need coding?
A long time ago, computers stored only English, a total of 26 English letters. And the smallest unit of information stored by a computer is 8 bits of a byte, which can represent 256 characters. This is enough for getting up early in English. Even adding some common symbols is enough.
Later, with the development of time, computers in China, Japan and other countries also began to flourish, so computers not only have to store English, but also began to store Chinese. But we all know that tens of thousands of words are too many to fit one byte. What should I do? One byte means no less than one byte, so just use a few more bytes. These bytes have to be converted to bit when they are stored, which is about to involve coding.
2. Coding mode
Many coding methods are provided in the calculation, such as ASCII, ISO-8859-1, GB2312, GBK, UTF-8, UTF-16 and so on. They set out the rules of transformation, according to which computers can correctly represent our characters.
Many ways such as GB2312, GBK, UTF-8, UTF-16 and so on can express Chinese characters. What's the difference between them?
(1) GB2312
It is a double-byte code, the total coding range is A1-F7, in which A1-A9 is a symbol area, containing a total of 682 symbols, and B0-F7 is a Chinese character area, including 6763 Chinese characters.
(2) GBK
Its coding range is 8140~FEFE (excluding XX7F) has a total of 23940 code points, it can represent 21003 Chinese characters.
(3) UTF-8
UTF-8 uses a variable length technique, and each coding region has a different code length. Different types of characters can be made up of 1 to 6 bytes.
3. Transcoding method * *
(1) IO flow
Here is the core answer to the interview question, the purpose of coding has been mentioned above, mainly the conversion between bytes and characters. Now that it's easy to talk about bytes and characters, we can think of IO streams in java. In other words, the transformation of the code in java is actually implemented by the classes in the IO stream.
The core is the above classes, of course, here is only part of the input, there are some output classes.
(2) String
Some transcoding methods are also provided in the String class. We will illustrate it with an example below. Why can String be implemented? This is because what is stored at the bottom of String is actually a byte, and String has a way to convert it directly to characters. So String must be able to do the same.
(3) Charset
This Charset is a class in javaNIO, and the whole process is to read the data and convert it to byte, that is, characters. Then re-encode it into characters and OK it.
Let's use the code to do this:
Code implementation 1. IO stream public void convertionFile () throws IOException {
File file = new File ("architect technology stack for D://fdd/java .txt")
FileInputStream fis = new FileInputStream (file)
InputStreamReader inReader = new InputStreamReader (fis, "gbk")
FileOutputStream fos = new FileOutputStream (file)
OutputStreamWriter outReader = new OutputStreamWriter (fos, "utf-8")
2. String public void convertionString () throws UnsupportedEncodingException {
String s = "architect's technology stack for java"
/ / the transcoding process under normal circumstances
Byte [] b = s.getBytes ("gbk"); / / Encoding
String sa = new String (b, "gbk"); / / Decoding
System.out.println (sa)
/ / the process of transcoding in error state
B = sa.getBytes ("utf-8"); / / Encoding
Sa = new String (b, "utf-8"); / / Decoding
System.err.println (sa)
3. Charset public void convertionCharset () throws IOException {
Charset charset = StandardCharsets.UTF_8
/ / create corresponding encoders and decoders from character sets
CharsetEncoder encoder = charset.newEncoder ()
CharsetDecoder decoder = charset.newDecoder ()
/ / construct a buffer
CharBuffer charBuffer = CharBuffer.allocate (64)
CharBuffer.put ('A')
CharBuffer.flip ()
/ / convert a character sequence to a byte sequence
ByteBuffer bb = encoder.encode (charBuffer)
/ / convert a byte sequence to a character sequence
Bb.flip ()
CharBuffer cb = decoder.decode (bb)
At this point, the study of "what are the coding conversion methods in java" 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.