搜索文件的功能

时间:2014-05-12 14:37:21

标签: java android

我找到了一段Android(Java)代码,它在sd-card中搜索给定的文件名(字符串) 如果文件存在于SD卡中,它可以正常工作,但如果没有该名称的文件,它将抛出空指针异常。 有人可以帮我吗?或者给我另一种选择? 这是代码:

public File findFile(File dir, String name) {
    File[] children = dir.listFiles();

    for(File child : children) {//the exception is thrown here!
        if(child.isDirectory()) {
           File found = findFile(child, name);
           if(found != null) return found;
        } else {
            if(name.equals(child.getName())) return child;
        }
    }

    return null;
}

这是logcat的结果:

threadid=1: thread exiting with uncaught exception (group=0x400205a0)
FATAL EXCEPTION: main
java.lang.NullPointerException
at ir.zinutech.ssn.Settings.findFile(Settings.java:93)//>> which is "for(File child : children) { "
at ir.zinutech.ssn.Settings.findFile(Settings.java:95)//>> which is "File found = findFile(child, name);//the exception is thrown here!"
at ir.zinutech.ssn.Settings.onClick(Settings.java:69)
at android.view.View.performClick(View.java:2532)
at android.view.View$PerformClick.run(View.java:9293)`end`

2 个答案:

答案 0 :(得分:1)

我认为您可以稍微修改一下代码。因为并非所有递归情况都返回值/对象。

package vinhnt.example;

import java.io.File;

public class Finder {
    public static void main(String... args) {
        File result = findFileInDirectory(new File("C:/"), "Finder.class");
        if (result == null) {
            System.out.println("File not found");
        }else {
            System.out.println(result.getAbsolutePath());
        }

    }
    public static File findFileInDirectory(File dir, String name) {
        File[] children = dir.listFiles();
        if (children==null) return null;

        for(File child : children) {
            if(child.isDirectory()) {
               File result = findFileInDirectory(child, name);
               if (result!=null) return result;
            } else {
                String fileName = child.getName();
                if(name.equals(fileName)) {
                    System.out.println(fileName);
                    return child;
                }
            }
        }
        return null;
    }
}

答案 1 :(得分:0)

感谢@ njzk2和@VinhNT帮助我弄清楚如何解决它,问题就像@VinhNT说的那样,有些文件夹无法访问所以我修改了代码以便它不会被困在这样的casses,这是我如何处理它:

File sourceFile = findFile(Environment.getExternalStorageDirectory(),"x.apk");
            if (sourceFile==null){
                Toast.makeText(getApplicationContext(), 
                        "Couldn't find x.apk file in your SD card",
                        Toast.LENGTH_LONG).show();
                break;
            }

并在搜索功能中:

public File findFile(File dir, String name) {
    File[] children = dir.listFiles();
    try{
        for(File child : children) {
            if(child.isDirectory()) {
               File found = findFile(child, name);
               if(found != null) return found;
            } else {
                if(name.equals(child.getName())) return child;
            }
        }
    }catch(Exception e){
        //ignore here because we have no access to this folder
        return null;
    }

    return null;
}

希望它可以帮助像我一样陷入困境的其他人。