章节选择器

来源:互联网 发布:湖北省软件行业协会 编辑:程序博客网 时间: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
原创粉丝点击