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!