技術分享 J2ME中讀取Unicode和UTF-8編碼檔案(1)

來源:互聯網
上載者:User

本文向大家簡單介紹一下J2ME中讀取Unicode和UTF-8編碼檔案的方法,與J2SE和J2EE相比,J2ME總體的的運行環境和目標更加多樣化,但其中每一種產品的用途卻更為單一,而且資源限制也更加嚴格。

J2ME中讀取Unicode和UTF-8編碼檔案

一、J2ME讀取Unicode檔案

 
  1. /**  
  2.  
  3. *讀取Unicode編碼文字檔  
  4.  
  5. *@paramresourceString-檔案名稱  
  6.  
  7. *@returnString-Unicode文本  
  8.  
  9. */  
  10.  
  11. publicstaticStringread_Uni(Stringresource){  
  12. byteword_uni[]=newbyte[1024];  
  13. StringstrReturn=null;  
  14. InputStreamis;  
  15. try{  
  16. is=instance.getClass().getResourceAsStream(resource);  
  17. is.skip(2);//跳過兩個位元組的檔案頭  
  18.  
  19. is.read(word_uni);  
  20. is.close();  
  21. StringBufferstringbuffer=newStringBuffer("");  
  22. for(intj=0;j<word_uni.length;){  
  23. intl=word_uni[j++];  
  24. inth=word_uni[j++];  
  25. charc=(char)((l&0xff)|((h<<8)&0xff00));  
  26. stringbuffer.append(c);  
  27. }  
  28. strReturn=stringbuffer.toString();  
  29. }catch(IOExceptionex){  
  30. System.out.println(ex);  
  31. }finally{  
  32. is=null;  
  33. }  
  34. returnstrReturn;  
  35. }  


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.