//采用GB2312编码的普通文件,用行和,来表示各个具体章节含义。读取index.txt文件,按照
//固定的格式读取,章节大小,章节的名字索引等信息,具体参见index.txt文件。
public void readIndexTxtFile() {
int chapterTotal = 0;
ChapterInfo model;
try {
InputStream inputstream = getClass().getResourceAsStream(
"/config/index.txt");
byte[] buf = new byte[(int) inputstream.available()];
try {
inputstream.read(buf);
inputstream.close();
inputstream=null;
} catch (IOException e) {
e.printStackTrace();
}
String result = null;
//result = new String(DataReader.UTF2Uni(buf,buf.length));
result = new String(buf,utf8);
buf=null;
String[] lines=split(result,"/r/n");
result=null;
if(lines.length<=2)
{
return;
}
chapterTotal=Integer.parseInt(lines[0]);//章节总数
authorString=lines[1].replace('|', '/n');//repalce:将就的字符用新字符代替,这里即用'/n'代替'|'
// int total = 0;
if (chapterTotal > 0)
{
chapterInfo = new ChapterInfo[chapterTotal];
for (int index = 0; index < chapterTotal; index++)
{
if ((model = new ChapterInfo()).readBySplit(lines[2+index], index))//readBySplit返回值为boolean类型
{
chapterInfo[index] = model;//model返回的是什么?是lines[0]吗?(model是ChapterInfo对象根据构造函数返回的应该是整个章节信息数组,包括章节索引,文本资源名称,章节名等)
// total++;
}
}
}
lines=null;
model=null;
System.gc();
return;
} catch (Exception _ex) {
chapterTotal = 0;
}
}
/**根据分隔符分割字符串为字符串数组*/
public static String[] split(String original,String regex) {
// System.out.println("original::"+original);
//取子串的起始位置
int startIndex = 0;
//将结果数据先放入Vector中
Vector v = new Vector();
//返回的结果字符串数组
String[] str = null;
//存储取子串时起始位置
int index = 0;
//获得匹配子串的位置
startIndex = original.indexOf(regex);
//如果起始字符串的位置小于字符串的长度,则证明没有取到字符串末尾。
//-1代表取到了末尾
while(startIndex < original.length() && startIndex != -1)
{
String temp = original.substring(index,startIndex);
//取子串
v.addElement(temp);
//设置取子串的起始位置
index = startIndex + regex.length();
//获得匹配子串的位置
startIndex = original.indexOf(regex,index);//设置起始新的查找位置startIndex + regex.length()
}
//取结束的子串
v.addElement(original.substring(index + 1 - regex.length()));//index + 1 - regex.length()?为何加1
// System.out.println("vector::"+v);
//将Vector对象转换成数组
str = new String[v.size()];
for(int i=0;i<v.size();i++)
{
str[i] = (String)v.elementAt(i);
}
//返回生成的数组
v=null;
System.gc();
return str;
}