Completely solve the problem of Java character encoding [00 original]

Source: Internet
Author: User
Tags id3 id3 tag
Recently, MP3 ID3 tags are being extracted, and Chinese character encoding is also encountered.
Remember to read text from a text file as follows:

/**
* Read text file content by line
* @ Param filename
* File path and file name
* @ Throws ioexception
*/
Public void readfile (string filename)
{
Try
{
Inputstream is = new fileinputstream (filename );

Bytearrayoutputstream byteout = new bytearrayoutputstream ();
Byte TMP [] = new byte [1024];
Byte context [];

Is. Read (TMP );
Byteout. Write (TMP );
Context = byteout. tobytearray ();
String STR = new string (context, "GBK ");
System. Out. println (STR );
}
Catch (filenotfoundexception fnfe)
{
Fnfe. printstacktrace ();
}
Catch (exception E)
{
E. printstacktrace ();
}
}

An error occurred while processing the ID3 tag in the same way. New String (context, "GBK"); garbled characters ....
I checked various materials and wrote a simple solution test, as shown below:

Package cn.edu. ynu. Sei. liva. test;

Public class charsetdecoder
{
Public static void main (string [] ARGs)
{
Try
{
String musicname = "maid ";
System. Out. println ("before decoding:" + musicname );
// 1. Convert musicname (by Unicode-> ISO-8859-1) back to the original byte [].
// 2. Because the original song name is in GBK encoding mode, the bytes [] is transcoded in GBK-> Unicode mode.
String musicnamedecode = new string (musicname. getbytes ("ISO-8859-1"), "GBK ");
System. Out. println ("After decoding:" + musicnamedecode );
}
Catch (exception E)
{
E. printstacktrace ();
}
}
}

Well, we can finally solve the Java character encoding problem!

Related Article

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.