如何在java中从文本文件读取数据,条件是数据必须从一个位置读取到另一个位置

如何在java中从文本文件读取数据,条件是数据必须从一个位置读取到另一个位置,java,buffer,text-files,Java,Buffer,Text Files,我想先将文本文件从事件1读取到事件1的结尾,然后从事件2读取到事件2的结尾,以此类推,代码中将有多行代码位于一个事件到另一个事件之间。 文本文件如下所示: event 1 arg arg arg end 1 event 2 arg arg arg end 2 如果我明白,试试看 public void method() { String file = "C:\\text.txt"; LineNumberReader reader = null; Pattern

我想先将文本文件从事件1读取到事件1的结尾,然后从事件2读取到事件2的结尾,以此类推,代码中将有多行代码位于一个事件到另一个事件之间。 文本文件如下所示:

event 1 arg  arg  arg  end 1 
event 2 arg arg arg end 2

如果我明白,试试看

public void method() {
    String file = "C:\\text.txt";
    LineNumberReader reader = null;
    Pattern pattern;
    Matcher matcher;
    String line=null;
    int i;
    try {
        reader = new LineNumberReader(new FileReader(file));

        while ((line = reader.readLine()) != null) {
            i = reader.getLineNumber();
            String template = "event" + i + ".+end" + i;
            pattern = Pattern.compile(template);
            matcher = pattern.matcher(line);
            if (matcher.find()) {
                System.out.println(line.substring((matcher.start()), matcher.end()));
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
档案中有

event1 arg arg arg end1
event2 arg arg arg end2

并输出相同的

如果我理解,请尝试

public void method() {
    String file = "C:\\text.txt";
    LineNumberReader reader = null;
    Pattern pattern;
    Matcher matcher;
    String line=null;
    int i;
    try {
        reader = new LineNumberReader(new FileReader(file));

        while ((line = reader.readLine()) != null) {
            i = reader.getLineNumber();
            String template = "event" + i + ".+end" + i;
            pattern = Pattern.compile(template);
            matcher = pattern.matcher(line);
            if (matcher.find()) {
                System.out.println(line.substring((matcher.start()), matcher.end()));
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
档案中有

event1 arg arg arg end1
event2 arg arg arg end2

然后输出相同的

那么,到目前为止,您的问题在哪里?到目前为止,您的问题在哪里?