Android-checks whether the TXT file contains double-byte characters, androidtxt
When reading double-byte characters, it mainly involves encoding selection:
Java code
- Public static boolean isRightfulTXT (File f ){
- // TODO Auto-generated method stub
- String regexp = "[^ \ x00-\ xff]"; // Double Byte Character
- Pattern p = Pattern. compile (regexp );
- Try {
- FileInputStream FCM = new FileInputStream (f );
- // The "GBK" encoding method supports double-byte characters
- InputStreamReader isr = new InputStreamReader (FCM, "GBK ");
- BufferedReader br = new BufferedReader (isr );
- String line = "";
- While (line = br. readLine ())! = Null ){
- // Read files row by row,
- // Search whether the file contains double-byte characters
- Matcher m = p. matcher (line );
- If (m. find ()){
- FCM. close ();
- Isr. close ();
- Br. close ();
- Return false;
- }
- }
- FCM. close ();
- Isr. close ();
- Br. close ();
- } Catch (FileNotFoundException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- } Catch (UnsupportedEncodingException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- } Catch (IOException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- Return true;
- }
The code above: checks whether the txt file contains double-byte characters. If any, false is returned. Otherwise, true is returned.