Completely solves the garbled characters of reading TXT and LRC from Android (automatically determining the document type and transcoding)

Source: Internet
Author: User

I have been studying Android players over the past few days. When I read the lyrics, I am always confused:

Today, I saw an article about file transcoding on the Internet, which completely solved my garbled problem, so that I don't have to manually transcode the LRC lyrics file, I would like to share with you this article (the principles of the conditions in the IF else are not very well understood. Please help me ):

Package com. qgmobile. utils;

Import java. Io. bufferedinputstream;
Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;

/**
* Encoding format of the converted file
* @ Author yangchuxi
*
*/
Public class convertfilecode {
Public String converfile (string filepath ){
System. Out. println ("convertfilecode --------->" + filepath );
File file = new file (filepath );
Fileinputstream FCM = NULL;
Bufferedinputstream Bis = NULL;
Bufferedreader reader = NULL;
String text = "";
Try {
FS = new fileinputstream (File );
Bis = new bufferedinputstream (FCM );
Bis. Mark (4 );
Byte [] first3bytes = new byte [3];
// System. Out. println ("");
// Locate the first three bytes of the document and automatically determine the document type.
Bis. Read (first3bytes );
Bis. Reset ();
If (first3bytes [0] = (byte) 0xef & first3bytes [1] = (byte) 0xbb
& First3bytes [2] = (byte) 0xbf) {// UTF-8

Reader = new bufferedreader (New inputstreamreader (BIS, "UTF-8 "));

} Else if (first3bytes [0] = (byte) 0xff
& First3bytes [1] = (byte) 0xfe ){

Reader = new bufferedreader (
New inputstreamreader (BIS, "Unicode "));
} Else if (first3bytes [0] = (byte) 0xfe
& First3bytes [1] = (byte) 0xff ){

Reader = new bufferedreader (New inputstreamreader (BIS,
"Utf-16be "));
} Else if (first3bytes [0] = (byte) 0xff
& First3bytes [1] = (byte) 0xff ){

Reader = new bufferedreader (New inputstreamreader (BIS,
"Utf-16le "));
} Else {

Reader = new bufferedreader (New inputstreamreader (BIS, "GBK "));
}
String STR = reader. Readline ();

While (STR! = NULL ){
TEXT = text + STR + "/N ";
STR = reader. Readline ();
}
System. Out. println ("text" + text );
} Catch (exception e ){
E. printstacktrace ();
} Finally {
If (FS! = NULL ){
Try {
FCM. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
If (Bis! = NULL ){
Try {
Bis. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}

Return text;

}
}

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.