Chinese garbled characters in Java Io streams

Source: Internet
Author: User

I believe that people who have learned javaee have encountered Chinese garbled characters. The simplest problem is that Chinese characters are garbled during form data submission. Is it difficult to solve the garbled characters? In fact, it is not difficult to grasp the encoding settings, as long as the side of the garbled magic horse goes.

Today, I will talk about some common functions that can be encoded. Let's talk about common coding, such as ISO-8859-1, GBK, UTF-8, etc. These three are the most common. First, to prevent garbled characters, we must first unify the encoding, and then use some functions to solve garbled characters.

The following describes common methods and examples of Chinese Garbled text. Since the form data contains Chinese Garbled text, let's take a look at the solution.

Function: New String (byte [] data, string encoding );

This method is mainly used to solve the Chinese garbled characters when the form data is submitted. This method is usually used with string. getbytes (string encoding.

Usage: String STR = new string (formmsg. getbytes ("ISO-8859-1"), "UTF-8 ");

Today, I want to talk about the solutions to Chinese garbled characters that may easily occur in I/O Stream operations, so let's get back to the point and talk about the garbled issues and solutions I encountered during my learning.

First, we all know that Java Io streams are divided into word throttling and byte streams by byte. We can see the literal meaning that byte streams are operated in bytes, the byte stream is operated by characters. The byte stream is generally used to operate some binary files, such as MP3 files and jpg files. What about ghost stream? It is generally used to operate some text files.

I/O Stream operations are also involved in many aspects of Android, and of course there are also many Chinese garbled characters. Some details may cause garbled characters if they are not noticed.

First, let's talk about byte streams. First, paste a method.

/** Read the file */Public String readerfile (file F) {string STR = ""; fileinputstream FCM = NULL; try {FD = new fileinputstream (f ); byte [] B = new byte [512]; int N; while (n = Fi. read (B ))! =-1) {STR = STR + new string (B, 0, n); B = new byte [512] ;}} catch (exception e) {e. printstacktrace ();} finally {try {FCM. close ();} catch (exception e) {e. printstacktrace () ;}} return STR ;}

This method reads the content in the file object and returns a string. If you copy this method to read files containing Chinese characters, Chinese characters may be garbled in case of exceptions, if Chinese characters are garbled, how can this problem be solved? In fact, the string class provides a solution. You only need to change STR = STR + new string (B, 0, n); To STR = STR + new string (B, 0, n, "GBK.

In general, the above method can read Chinese characters, but think about it carefully. After all, the above method is in bytes, while a Chinese character occupies two bytes, careful students should have thought of it. The above method reads 512 bytes at a time. If a Chinese character occupies 512nd bytes and 513rd bytes, when you read 512 bytes at a time, you have to break them up and create a new string. Do you say garbled characters are not garbled?

Therefore, operations on files containing Chinese characters are still done through the upstreaming stream, and the upstreaming stream is fast, although upgrading from the byte stream step by step to the upstreaming stream is annoying ..

Paste a method first.

 
/** Upgrade to read the file */Public String readerfile (file F) {string STR = ""; string line = ""; fileinputstream FCM = NULL; inputstreamreader ISR = NULL; bufferedreader BR = NULL; try {FD = new fileinputstream (f); ISR = new inputstreamreader (ISI); BR = new bufferedreader (ISR); While (line = BR. readline ())! = NULL) {STR = STR + line;} catch (exception e) {e. printstacktrace ();} finally {try {FCM. close ();} catch (exception e) {e. printstacktrace () ;}} return STR ;}

If garbled characters still occur after the above method is used, what can you do? Where can I set the correct encoding?

Will you learn the first method? STR = STR + new string (line. getbytes ("ISO-8859-1"), "GBK ");? You can try it on your own --.

So you will learn the second method? STR = STR + new string (line, "GBK ");? There is no such method at all...

So what should we do?

In fact, there are many places in Java where encoding can be set. In the above solution, you only need to change ISR = new inputstreamreader (FCM) to ISR = new inputstreamreader (FCM, "GBK "); you can do it.

There are still many Chinese garbled characters in Java. This is just a flaw. There are more solutions for you to find out. Remember to share them...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.