Recently in the MP3 ID3 tag extraction, but also encountered the problem of encoding Chinese characters.
Remember when you read the Chinese text from a text file, the following method looks like this:
/**
* Read text file content by line
* @param fileName
* File path and filename
* @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 ();
}
}
The same approach to handling ID3 tags is problematic. New String (Context, "GBK"), out or garbled ....
consulted a variety of materials, wrote a simple solution to the test, as follows:
Package cn.edu.ynu.sei.liva.test;
public class Charsetdecoder
{
public static void Main (string[] args)
{
Try
{
String musicname = "Óâ¸òµäðä";
System.out.println ("Before decoding:" + musicname);
1. Turn musicname (press unicode->iso-8859-1) back to the original byte[].
2. Because the original song name is GBK encoding way, so put bytes[] according to the Gbk->unicode Way of transcoding
String musicnamedecode = new String (Musicname.getbytes ("iso-8859-1"), "GBK");
System.out.println ("After decoding:" + Musicnamedecode);
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}
Oh, finally can completely solve the Java character coding problem.
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.