java模拟Unix命令grep操作
/**
* 模拟Unix命令grep操作,输出行号和行内容
* @param path 待查询的文件路径
* @param query 待查询的字符串
* @throws IOException
**/
public static void grep(String path,String query) throws IOException{
int lineno=0;
String line = null;
FileReader in=new FileReader(path);
BufferedReader br=new BufferedReader(in);
System.out.println("所需处理的文件:"+path);
System.out.println("行号-------------"+"包含' "+query+" '的行内容");
line=br.readLine();
while(line!=null){
lineno++;
if(line.indexOf(query)!=-1)
System.out.println(lineno+"---------------"+line);
line=br.readLine();
}
}