private String mBookTitle;
private Bitmap mBookCover;
//格式化文本,将文本以段落为单位保存
private List<String> mParagraphList;
//目录集合(卷/章/回/集等)
private List<String> mBookContents;
//目录对应的在段落集合中的索引
private List<Integer> mContentParaIndexs;
//空两格
private String mSpace = "\\t\\t\\t\\t\\t\\t";
public Book(String bookTitle, Bitmap bookCover, String fullText) {
mParagraphList = new ArrayList<>();
mBookContents = new ArrayList<>();
mContentParaIndexs=new ArrayList<>();
mBookTitle = bookTitle;
mBookCover = bookCover;
formatText(fullText);
findContents(mParagraphList);
}
//格式化文本,将文本以段落为单位保存
private void formatText(String text) {
boolean isFirstParas = true;
String paragraph = "";
//按段落切分文本
String\[\] paragraphs = text.split("\\\\s{2,}");
//格式化段落
for (int i = 0; i < paragraphs.length; i++) {
if (paragraphs\[i\].isEmpty()) {
continue;
}
if (isFirstParas) {
paragraph = mSpace + paragraphs\[i\];
isFirstParas = false;
} else {
paragraph = "\\n" + mSpace + paragraphs\[i\];
}
mParagraphList.add(paragraph);
}
}
private void findContents(List<String> paraList) {
//字符串匹配模式
String patternString = "第\\\\S{2,4}\\\\s\\\\S{2,}";
Pattern pattern = Pattern.compile(patternString);
for (String para:paraList) {
Matcher matcher = pattern.matcher(para);
if (matcher.find()){
//除去段首多余空格
int start = matcher.start();
int end = matcher.end();
String subString = para.substring(start, end);
mBookContents.add(subString); //目录
mContentParaIndexs.add(paraList.indexOf(para)); //目录对应的在段落集合中的索引
}
}
}
public String getBookTitle() {
return mBookTitle;
}
public Bitmap getBookCover() {
return mBookCover;
}
public List<String> getParagraphList() {
return mParagraphList;
}
public List<String> getBookContents() {
return mBookContents;
}
public List<Integer> getContentParaIndexs() {
return mContentParaIndexs;
}
}
加载类:
public class BookLab {
public static final String TEXT = "text";
public static final String IMAGE = "image";
private static BookLab sBookLab;
private AssetManager mAssetManager;
private List<Book> mBookList;
//assets中的文件名清单
private String\[\] mAssetsImageList;
private String\[\] mAssetsTextList;
private BookLab(Context context) {
mAssetManager = context.getAssets();
loadAssetsFiles();
}
public static BookLab newInstance(Context context) {
if (sBookLab == null) {
sBookLab = new BookLab(context);
}
return sBookLab;
}
//加载assets中的文件
private void loadAssetsFiles() {
mBookList = new ArrayList<>();
//获取image、text中的文件名清单
try {
mAssetsImageList = mAssetManager.list(IMAGE);
mAssetsTextList = mAssetManager.list(TEXT);
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < mAssetsTextList.length; i++) {
//获取书名
String\[\] nameSplit = mAssetsTextList\[i\].split("\_");
String nameSecond = nameSplit\[nameSplit.length - 1\];
String bookTitle = nameSecond.replace(".txt", "");
//获取封面
String imagePath = IMAGE + "/" + mAssetsImageList\[i\];
Bitmap bookCover = loadImage(imagePath);
//获取文本
String textPath = TEXT + "/" + mAssetsTextList\[i\];
String bodyText = loadText(textPath);
Book book = new Book(bookTitle, bookCover, bodyText);
mBookList.add(book);
}
}
//从assets中读取文本
private String loadText(String path) {
InputStream in = null;
BufferedReader reader = null;
StringBuilder stringBuilder = new StringBuilder();
try {
in = mAssetManager.open(path);
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder.toString();
}
//从assets中读取图片