Home > Mobile >  Android novel readers how to identify different coding to overcome
Android novel readers how to identify different coding to overcome

Time:09-17

Only ANSI encoding of the text to open, the rest of the code will cause the code


The code is as follows:
Private void saveChapter (File book, String md5) throws IOException {
String regex="chapter first. {1, 7}. {0} \ r \ n";

The String encoding;
InputStream fis.
String inn;

FileInputStream fiss=new FileInputStream (book);
Inn=readShaderFromRawResource (fiss);
Fis=new ByteArrayInputStream (inn. GetBytes (" utf-8 "));
Byte [] buf=new byte [4096].
UniversalDetector detector=new UniversalDetector (null);
Int nread;
While ((nread=fis. Read (buf)) & gt; 0 & amp; & ! The detector. IsDone ()) {
The detector. The handleData (buf, 0, nread);
}
The detector. DataEnd ();
Encoding=detector. GetDetectedCharset ();
If (encoding==null | | encoding. The length ()==0)
Encoding="utf-8";
Fis. Close ();
Fis=null;

Int chapterPageIndex=0;
String title=null;
The StringBuilder contentBuilder=new StringBuilder ();
Fiss=new FileInputStream (book);
Inn=readShaderFromRawResource (fiss);
Fis=new ByteArrayInputStream (inn. GetBytes (" utf-8 "));
InputStreamReader inputreader=new InputStreamReader (fis, encoding);
BufferedReader buffreader=new BufferedReader (inputreader);
The String line;
While ((line=buffreader readLine ())!=null) {
The Pattern p=Pattern.com running (regex);
The Matcher m=p. atcher (line);
If (m. ind ()) {
String temp=line. Trim (). The substring (0, line. Trim (). The indexOf (" first "));
If (temp!=null & amp; & Temp. The trim (). The length () & gt; 0 {
ContentBuilder. Append (temp);

}
If (contentBuilder. ToString (). The length () & gt; 0 {
If (contentBuilder. ToString (). ReplaceAll (" ", ""). The trim (), length () & gt; 0 {

SaveDurChapterContent (md5, chapterPageIndex, title, contentBuilder. ToString ());
ChapterPageIndex++;
}
ContentBuilder. Delete (0, contentBuilder. Length ());
}
Title=line. Trim (). The substring (line. The trim (). The indexOf (" first "));

} else {
If (line. Trim (). The length ()==0) {
If (contentBuilder. Length () & gt; 0 {

ContentBuilder. Append (" \ r \ n \ u3000 \ u3000 ");

} else {
ContentBuilder. Append (" \ r \ u3000 \ u3000 ");

}
} else {

String lines=line. Replace (" ", "\ r \ n");

ContentBuilder. Append (lines);

If (title==null) {
Title=line. Trim ();

}
}
}
}
If (contentBuilder. Length () & gt; 0 {
SaveDurChapterContent (md5, chapterPageIndex, title, contentBuilder. ToString ());
ContentBuilder. Delete (0, contentBuilder. Length ());
Title=null;
}
Buffreader. Close ();
Inputreader. Close ();
Fis. Close ();
Fis=null;
}
  • Related