java如何提取txt小说中的章节目录并记录位置?
公共静态void readTxtFile(字符串文件路径){
尝试{
字符串编码= " UTF-8 ";
File file =新文件(File path);
if(file . is file()& amp;& ampFile.exists()) {//判断文件是否存在。
InputStreamReader read = new InputStreamReader(
new FileInputStream(文件),编码);//考虑到编码格式
buffered reader buffered reader = new buffered reader(read);
String lineTxt = null
int offset = 0;//章节所在的行数
int count = 1;//章节数
列表& ltInfoVo & gtlist = new ArrayList & ltInfoVo & gt();
InfoVo infoVo
while((line txt = buffered reader . readline())!= null) {
info VO = new info VO();
offset++;
If (lineTxt.contains("第一")&;& ampLineTxt.contains("章")) (
infoVo.setCount(计数);
infoVo.setOffset(偏移量);
infovo . settitle(line txt);
list . add(infoVo);
count++;
}
}
system . out . println(list . size());
System.out.println(list.get(0))。getCount());
System.out.println(list.get(0))。get offset());
System.out.println(list.get(0))。getTitle());
read . close();
}否则{
System.out.println("找不到指定文件");
}
} catch(异常e) {
System.out.println("读取文件内容时出错");
e . printstacktrace();
}
}
公共静态void main(String[] args) {
//console . main menu();
string file path = " C:\ \ 20130815 . txt ";
readTxtFile(文件路径);
}
InfoVo结构:
公共类InfoVo {
私有整数计数;
私有整数偏移量;
私有字符串标题;
公共整数getCount() {
返回计数;
}
public void setCount(整数计数){
this.count = count
}
公共整数getOffset() {
返回偏移量;
}
公共void setOffset(整数偏移量){
this.offset = offset
}
公共字符串getTitle() {
返回标题;
}
公共void setTitle(字符串标题){
this.title = title
}
}