-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindAll.java
More file actions
72 lines (69 loc) · 1.52 KB
/
Copy pathFindAll.java
File metadata and controls
72 lines (69 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
public class FindAll{
public static void main(String[] args){
if(args.length!=2){
System.err.println("usage: java FindAll scope keyword");
return;
}
if(!findAll(new File(args[0]), args[1])){
System.err.println(args[0] + "is not a directory");
}
}
static boolean findAll(File dir, String keyword){
File[] files = dir.listFiles();
if(files == null){
// Not a directory or IO error
return false;
}
File tempFile;
for(int i=0; i<files.length; i++){
tempFile = files[i];
if(tempFile.isDirectory()){
findAll(tempFile, keyword); // invoke this method recursively
}else{
if(find(tempFile, keyword)){
System.out.println(tempFile.getPath());
}
}
}
return true;
}
static boolean find(File file, String keyword){
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(file));
int ch;
outer_loop:
do{
if((ch=br.read())==-1){
return false;
}
if(ch==keyword.charAt(0)){ //check the head character
for(int i=1;i<keyword.length();i++){
if((ch=br.read())==-1){
return false;
}
if(ch!=keyword.charAt(i)){
continue outer_loop;
}
}
return true; // match for one found
}
}while(true);
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
return false;
}
}