- private static String findFile(String filePath, String fileName)
- throws IOException {
- // TODO Auto-generated method stub
- File file = new File(filePath);
- if (!file.isDirectory()) {
- // print(file);
- } else if (file.isDirectory()) {
- // print("文件夹");
- String[] filelist = file.list();
- String name;
- for (int i = 0; i < filelist.length; i++) {
- file = new File(filePath + "/" + filelist[i]);
- if (!file.isDirectory()) {
- // print(file);
- name = file.getName();
- if (fileName.equals(name)) {
- //这里的result一定不要在方法里面定义,不然得不数据。
- result = readFileByLine(file.getAbsolutePath());
- break;
- }
- // print(file.getAbsolutePath());
- } else if (file.isDirectory()) {// 进行递归
- findFile(filePath + "/" + filelist[i], fileName);
- }
- }
- }
- print("* "+result);
- return result;
- }
尽量用全局变量保存递归中运算得到的结果!全局变量使用完后要进行清空处理,避免下次使用时,在未得到正确的数据的情况下误用上次得到的数据!!