基本结构吧,如图:
实现该功能的为Read类,list为成员变量,存放需要查找的特定字符创,包括四个方法:
public String findOneFile(File f){
StringBuffer record = new StringBuffer();
if(f.isDirectory()){
return null;
}
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String pline = br.readLine();
while(pline != null){
String onefile = findAll***(pline,f);
if(onefile.trim() != null)
if(record.toString().indexOf(onefile) == -1)
record.append(onefile);
pline = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(IOException e1){
e1.printStackTrace();
}
return record.toString() ;
}
public String findAll***(String pline,File f){
//int flag = 0 ;
StringBuffer sb =new StringBuffer();
for(int i=0 ; i < list.size(); i++){
int j = pline.indexOf(list.get(i).trim());
if(j>-1){
sb.append("what you find?---> "+list.get(i).trim()+"\t"+f.getName()+"\t\n") ;
}
continue ;
}
return sb.toString();
}
程序代码:
public class Read {
public static final List<String> list = new ArrayList<String>(Arrays.asList( 字符串集 ... ));
public String[] getFiles(File f){
if(f.isDirectory()){
return f.list();
}
return null;
}
public String findAllFile(String [] files){
StringBuffer result = new StringBuffer();
for(int i=0 ; i<files.length ; i++)
{
if(files[i].matches("abc*")){
File nowFile = new File("log"+System.getProperty("file.separator")+files[i]);
String allfile = findOneFile(nowFile);
if(allfile.trim() != null)
result.append(allfile);
}
}
return result.toString();
}
public String findOneFile(File f){
StringBuffer record = new StringBuffer();
if(f.isDirectory()){
return null;
}
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String pline = br.readLine();
while(pline != null){
String onefile = findAll***(pline,f);
if(onefile.trim() != null)
if(record.toString().indexOf(onefile) == -1)
record.append(onefile);
pline = br.readLine();
}
br.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(IOException e1){
e1.printStackTrace();
}
return record.toString() ;
}
public String findAll***(String pline,File f){
//int flag = 0 ;
StringBuffer sb =new StringBuffer();
for(int i=0 ; i < list.size(); i++){
int j = pline.indexOf(list.get(i).trim());
if(j>-1){
sb.append("what you find?---> "+list.get(i).trim()+"\t"+f.getName()+"\t\n") ;
}
continue ;
}
return sb.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Read read = new Read();
File directory =new File("目录名");
File result = new File("result.txt");
String resultString = null;
String[] files=read.getFiles(directory);
if(files != null)
resultString = read.findAllFile(files);
try{
BufferedWriter bw = new BufferedWriter(new FileWriter(result,false));
bw.write(resultString);
bw.flush();
bw.close();
}catch(IOException e){
e.printStackTrace();
}}
}