日期:2014-05-20 浏览次数:20748 次
//类似于此 public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new FileReader("D:\\workspace\\test\\src\\test.txt")); boolean start = false; StringBuilder sb = new StringBuilder(); while(true){ String line = reader.readLine(); if(line == null) break; if(!start){ if(line.indexOf("AAA") != -1){ line = line.substring(line.indexOf("AAA")); sb.append(line).append("\n"); start = true; continue; } }else{ if(line.indexOf("BBB") != -1){ line = line.substring(0, line.indexOf("BBB") + "BBB".length()); sb.append(line).append("\n"); start = false; break; }else{ sb.append(line).append("\n"); } } } System.out.println(sb.toString()); }
------解决方案--------------------
import java.io.*;
public class Demo1 {
public static String read()
{
FileReader fr=null;
BufferedReader br=null;
try
{
fr=new FileReader("d://Demo.txt");
br=new BufferedReader(fr);
String s="";
StringBuffer str =new StringBuffer();
while((s=br.readLine())!=null)
{
if(s.equals("AAA"))
{
while(!s.equals("BBB"))
{
str.append(s+"\r\n");
s=br.readLine();
}
return str.append("BBB").toString();
}
}
}
catch(Exception e)
{
e.getStackTrace();
}
finally
{
try
{
br.close();
fr.close();
}
catch(Exception e)
{
e.getStackTrace();
}
}
return null;
}
public static void main(String[] args) {
String s=read();
System.out.println(s);
}
}