import java.io.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class traverseDir {
public static void main(String[] args) {
if(args.length != 2) {
System.out.println("Invalid parameters!");
return;
}
String scanDir = args[0];
String packageSuffix = args[1];
try {
traverseDir td = new traverseDir();
lf.listFile(scanDir, packageSuffix);
} catch (ArrayIndexOutOfBoundsException ea) {
System.out.println("Usage: ListFiles <source dir> <target file>");
} catch (Exception e) {
System.out.println("IO error!/r/n"+e.toString());
}
}
public void listFile(String rp, String modifyTag){
File file=new File(rp);
File list[]=file.listFiles();
for(int i=0;i<list.length;i++) {
try {
if (list[i].isDirectory()) {
new traverseDir().listFile(list[i].toString(), modifyTag);
}
else {
if(isJavaFile(list[i].toString())) {
// replace(modifyTag, list[i].toString()); //read a file and do our work here.
}
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Access deny"+list[i].getAbsolutePath());
}
}
}
public void replace(String strReplacement, String filePath) throws FileNotFoundException, UnsupportedEncodingException {
File file = new File(filePath);
file.setWritable(true);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
StringBuffer strBuf = new StringBuffer();
try {
String line = null;
while((line = br.readLine()) != null) {
//System.out.println(line);
//import com.android.google.R.
Pattern pattern = Pattern.compile("^[ ]*import//scom.android.google.R[//s]*;[//s]*$");
Matcher m = pattern.matcher(line);
if(m.find()) {
//System.out.println("found: " + line);
line = "import com.android.google.R."+strReplacement+".R;"; // replace the found line here.
}
strBuf.append(line+"/r/n");
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream wf = new FileOutputStream(file);
try {
wf.write(strBuf.toString().getBytes());
} catch(IOException e) {
e.printStackTrace();
}
}
public boolean isJavaFile(String filePath) {
if(filePath.trim().endsWith(".java")) {
return true;
}
return false;
}
}
traverse a dir, search some text and do some thing
最新推荐文章于 2024-09-02 21:51:40 发布