This article briefly introduces how to read Unicode and UTF-8 encoded files in J2SE. Compared with J2SE and J2EE, the overall runtime environment and objectives of J2SE are more diversified, however, each product has a more single purpose and stricter resource restrictions.
Reading Unicode and UTF-8 encoding files in j2s
1. Reading Unicode files using j2's
- /**
-
- * Read Unicode-encoded text files
-
- * @ ParamresourceString-file name
-
- * @ ReturnString-Unicode text
-
- */
-
- PublicstaticStringread_Uni (Stringresource ){
- Byteword_uni [] = newbyte [1024];
- StringstrReturn = null;
- InputStreamis;
- Try {
- Is = instance. getClass (). getResourceAsStream (resource );
- Is. skip (2); // skip the two-Byte File Header
-
- Is. read (word_uni );
- Is. close ();
- StringBufferstringbuffer = newStringBuffer ("");
- For (intj = 0; j <word_uni.length ;){
- Intl = word_uni [j ++];
- Inth = word_uni [j ++];
- Charc = (char) (l & 0xff) | (h <8) & 0xff00 ));
- Stringbuffer. append (c );
- }
- StrReturn = stringbuffer. toString ();
- } Catch (IOExceptionex ){
- System. out. println (ex );
- } Finally {
- Is = null;
- }
- ReturnstrReturn;
- }