Android——檢測TXT檔案中是否含有雙位元組字元

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   sp   java   檔案   

在讀取雙位元組字元時,主要涉及到編碼的選取:

Java代碼  
  1. public static boolean isRightfulTXT(File f) {  
  2.   // TODO Auto-generated method stub  
  3.   String regexp="[^\\x00-\\xff]";//雙位元組字元  
  4.   Pattern p=Pattern.compile(regexp);  
  5.     
  6.   try {  
  7.    FileInputStream fis=new FileInputStream(f);  
  8.    //"GBK"編碼方式支援雙位元組字元  
  9.    InputStreamReader isr=new InputStreamReader(fis, "GBK");  
  10.    BufferedReader br=new BufferedReader(isr);  
  11.    String line="";  
  12.    while((line=br.readLine())!=null){  
  13.     //逐行讀取檔案,  
  14.     //檢索檔案中是否含有雙位元組字元  
  15.     Matcher m=p.matcher(line);  
  16.     if(m.find()){  
  17.      fis.close();  
  18.      isr.close();  
  19.      br.close();  
  20.      return false;  
  21.     }    
  22.    }    
  23.    fis.close();  
  24.    isr.close();  
  25.    br.close();  
  26.   } catch (FileNotFoundException e) {  
  27.    // TODO Auto-generated catch block  
  28.      
  29.    e.printStackTrace();  
  30.   } catch (UnsupportedEncodingException e) {  
  31.    // TODO Auto-generated catch block  
  32.    e.printStackTrace();  
  33.   } catch (IOException e) {  
  34.    // TODO Auto-generated catch block  
  35.    e.printStackTrace();  
  36.   }  
  37.    
  38.   return true;    
  39.  }  

 以上代碼功能:檢測txt檔案中是否含有雙位元組字元,若有返回假,否則返回真。

Android——檢測TXT檔案中是否含有雙位元組字元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.