When reading double-byte characters, it mainly involves the selection of encodings:
Java code
- Public Static Boolean isrightfultxt (File f) {
- //TODO auto-generated method stub
- String regexp="[^\\x00-\\xff]"; Double-byte characters
- Pattern P=pattern.compile (regexp);
- try {
- FileInputStream fis=New FileInputStream (f);
- //"GBK" encoding supports double-byte characters
- InputStreamReader isr=New InputStreamReader (FIS, "GBK");
- BufferedReader br=New BufferedReader (ISR);
- String line="";
- While ((Line=br.readline ()) =null) {
- //Read file by line,
- //Retrieves whether the file contains double-byte characters
- Matcher M=p.matcher (line);
- if (M.find ()) {
- Fis.close ();
- Isr.close ();
- Br.close ();
- return false;
- }
- }
- Fis.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 above code function: detect whether the TXT file contains double-byte characters, if there is a return false, otherwise return true.
android--detect if the TXT file contains double-byte characters