/** * constructor * @param fpath the specified file (path)name */ public FindLine(String fpath){ file = new File(fpath); result = new ArrayList<String>(); }
/** * perform the function of finding the line which containing the specified * string * @param str the specified string contained in the line * @return the line that contains the specified str * or null,if no line contains such string */ public Collection find(String str){ try{ BufferedReader br = new BufferedReader(new FileReader(file)); String tmp = new String(); while((tmp=br.readLine())!=null){ if(tmp.contains(str)){ //return tmp; result.add(tmp); } } }catch(IOException e){ e.printStackTrace(); }
return result; }
/** * performed the find function,the specified string is initialized to * "LT:90" * @return the line that contains the specified str * or null,if no line contains such string */ public Collection find(){ return find(TOFIND); }
/** * the main function * @param args ... */ public static void main(String[] args){ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String name = new String(); System.out.print("intput the file name: "); try{ name = br.readLine(); if(name.lastIndexOf(".txt")==-1){ name += ".txt"; } }catch(IOException e){ e.printStackTrace(); System.exit(1); }
//下面那个是1.5版本的,如果你是1.5以下的,把<String>去掉就可以了import java.io.*;
import java.util.*;public class FindLine {
private File file;
private static final String TOFIND = "LT:90";
private Collection <String>result;
/**
* constructor
* @param fpath the specified file (path)name
*/
public FindLine(String fpath){
file = new File(fpath);
result = new ArrayList<String>();
}
/**
* perform the function of finding the line which containing the specified
* string
* @param str the specified string contained in the line
* @return the line that contains the specified str
* or null,if no line contains such string
*/
public Collection find(String str){
try{
BufferedReader br = new BufferedReader(new FileReader(file));
String tmp = new String();
while((tmp=br.readLine())!=null){
if(tmp.contains(str)){
//return tmp;
result.add(tmp);
}
}
}catch(IOException e){
e.printStackTrace();
}
return result;
}
/**
* performed the find function,the specified string is initialized to
* "LT:90"
* @return the line that contains the specified str
* or null,if no line contains such string
*/
public Collection find(){
return find(TOFIND);
}
/**
* the main function
* @param args ...
*/
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = new String();
System.out.print("intput the file name: ");
try{
name = br.readLine();
if(name.lastIndexOf(".txt")==-1){
name += ".txt";
}
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
System.out.println(new FindLine(".\\"+name).find());
}
}