假设d盘根目录下有一个test.txt文件,文件内容为:标题:什么都有
格式:txt
date:2008-11-24
【青春心】
...
挑战极限
突破界限到无限
用好本领显我身手
...
time:21:32
csdn讨论组
end如何提取从date之后到time之前的内容(有很多行)?public class Test{
  public static void main(String[] args){
    BufferedReader br = new BufferedReader(new FileReader("d:\\test.txt"));
    接下来怎么写?
  } 
}

解决方案 »

  1.   

    没测试过, 应该可以:
    String line = br.readLine();
    boolean done = false;
    while( (line = br.readLine()) != null) {
    if (line.startsWith("date:")) {
    while (line = br.readLine() != null) {
    if (line.startsWith("time:") {
    done = true;
    break;
    }
    else {
    System.out.println(line);
    }
    }
    }
    if (done) {
    break;
    }
    }
      

  2.   

    测试通过.代码如下: public String getPatternString(String filePath) throws Exception{
    String str = "";
    File file = new File(filePath);
    if(file.exists()) {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new FileReader(file));
    String oneLine = "";
    String state = "ending";
    while((oneLine=br.readLine()) != null) {
    if(oneLine.startsWith("date:")) {
    state = "reading";
    continue;
    }
    if(oneLine.startsWith("time:")) state = "ending";
    if(state.equals("reading")) sb.append(oneLine.trim()).append("\r\n");
    }
    str = sb.toString();
    }
    return str;
    } public static void main(String[] args) {
    PatternTest test = new PatternTest();
    try{
        System.out.println(test.getPatternString("d:/test.txt"));
    }catch(Exception e) {
    e.printStackTrace();
    }
    }
      

  3.   


    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class ReadandWrite {
    public static String method1(){
     Scanner scanner = null;
     String s = "";  
            try {
                scanner = new Scanner(new File("E:\\temp\\www.txt"));
                while (scanner.hasNext()) {
                    String str = scanner.next();
                    if(str.equals("time:21:32")){
                     break;
                    }
                    s =s+ str+"\n";
                }
            } catch(IOException e) {
                e.printStackTrace();
            } finally {
                scanner.close();
            }
            return s;
    }

    public static void main(String args[]){
    System.out.print(ReadandWrite.method1());
    }
    }这样测试可以
      

  4.   

    while( (line = br.readLine()) != null)
    1楼的 这句 有错
      

  5.   

    一搂的定义时有点错误:
    String line = "";
    while((line==br.readLine())!=null){
     ...........
    }