如何使用IO流读取txt文件指定的文本内容,如txt文件里的内容:456456<--begin-->axlknxkln<--end-->2562
,只要读取<--begin-->和<--end-->
标记里面的内容axlknxkln,写到另一个文件里。
麻烦帮忙使用java实现。

解决方案 »

  1.   

    String s = "";
    String mulu = "d:\\123.txt";
    FileReader fr = null;
    String begin = "<--begin-->";
    String end = "<--end-->";
    try {
    fr = new FileReader(mulu);
    BufferedReader br = new BufferedReader(fr);
    while ((s = br.readLine()) != null) {
    System.out.println(s.subSequence(
    s.indexOf(begin) + begin.length(), s.indexOf(end)));
    }
    fr.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  2.   

    使用字节流byte[] b = new byte[1024];
    不可以使用readLine()一行行的去读的。
      

  3.   

    不能用readLine() 太奇怪了                 String s = "";
    String mulu = "d:\\123.txt";
    FileReader fr = null;
    String begin = "<--begin-->";
    String end = "<--end-->";
    try {
    File f = new File(mulu);
    FileInputStream fis = new FileInputStream(f);
    byte[] b = new byte[1024];
    while (fis.read(b) != -1) {
    s = s + new String(b);
    while (s.indexOf(begin) != -1) {
    s = s.substring(s.indexOf(begin) + begin.length());
    if (s.indexOf(end) != -1) {
    System.out.println(s.substring(0, indexOf(end)));
    }
    }
    }
    fis.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  4.   

    String s = "";
    String mulu = "d:\\123.txt";
    FileReader fr = null;
    String begin = "<--begin-->";
    String end = "<--end-->";
    try {
    File f = new File(mulu);
    FileInputStream fis = new FileInputStream(f);
    byte[] b = new byte[1024];
    while (fis.read(b) != -1) {
    s = s + new String(b);
    while (s.indexOf(begin) != -1) {
    s = s.substring(s.indexOf(begin) + begin.length());
    if (s.indexOf(end) != -1) {
    System.out.println(s.substring(0, s.indexOf(end)));
    }
    }
    }
    fis.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    代码都不能插入了?  奇怪了