章节选择器
来源:互联网 发布:湖北省软件行业协会 编辑:程序博客网 时间:2024/05/02 05:52
章节选择器采用正则表达式来实现,本程序是用自己的小说文档来实现的,所以编译模式的设置要根据自己采用什么样的文档:
public class Test06 { public static void main(String[] args) { Pattern pattern = Pattern.compile("^\\s*第[一二三四五六七八九十零百千万]+章");//编译模式 try (RandomAccessFile raf = new RandomAccessFile("res/Utf.txt", "rw")) { byte[] bytes = new byte[102400]; int length; Map<Integer, Long> map = new HashMap<>(); int index = 1; while ((length = raf.read(bytes)) != -1) { String unicode = new String(bytes, 0, length, "Unicode"); String[] split = unicode.split("\r\n"); for (int i = 0; i < split.length - 1; i++) { Matcher matcher = pattern.matcher(split[i]); if (matcher.find()) { System.out.println(split[i]); map.put(index++, raf.getFilePointer() - unicode.substring(unicode.indexOf(split[i])).getBytes("Unicode").length); } } if (length == 102400) { raf.seek(raf.getFilePointer() - split[split.length - 1].getBytes("Unicode").length); } } BufferedReader sbr= new BufferedReader(new InputStreamReader(System.in)); System.out.println(index); int chapter = Integer.parseInt(sbr.readLine()); Long current = map.get(chapter); Long next = map.get(chapter + 1); raf.seek(current); if (next == null) { while ((length = raf.read(bytes)) != -1) { System.out.print(new String(bytes, 0, length, "Unicode" )); } } else { byte[] b = new byte[(int) (next - current)]; raf.read(b); System.out.println(new String(b, "Unicode")); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}运行结果:
输入章节数字:12
0 0
- 章节选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器
- 选择器(
- $选择器
- 选择器
- 选择器
- notebook
- LocalBroadcastManager发送本地广播
- 《TCP/IP详解 卷1:协议》读书笔记 第2章 链路层
- android OrmLite 实际应用
- 【Android学习笔记】LinearLayout嵌套RecycleView后无法点击,onClick失效的问题
- 章节选择器
- 用busybox制作yaffs2文件系统
- el表达式里面取List中的第n个元素
- Linux Signal及Golang中的信号处理
- CGContext含义
- Opensuse编译内核代码
- 可拉伸头部控件
- 【HDOJ 5794】A Simple Chess(大组合数Lucas定理+容斥)
- Linux网络编程【1】