package practice;
import java.io.*;
import java.util.Date;
public class Chapter8 {
void check(String filename,String str)throws Exception {
File f=new File(filename); //封装文件
BufferedReader br=new BufferedReader(new FileReader(f));
String s;
while((s=br.readLine())!=null) {
if(s.indexOf(str)>=0) System.out.println(s); //存在str子串则输出此行
}
}
public static void main(String []args){
if(args.length!=2) {
System.out.println("使用方式错误!");
System.exit(1);
}
try {
Chapter8 c=new Chapter8();
c.check(args[0], args[1]);
}catch(Exception e) {
e.getMessage();
System.out.println(e.getClass().getName());
}
}
}
文件文本
输出结果