【问题标题】:search function for files文件搜索功能
【发布时间】:2014-05-12 14:37:20
【问题描述】:

我找到了一段 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`

【问题讨论】:

  • 发布堆栈跟踪。无法在您指示的行处引发异常,因为没有调用空对象。 (唯一的调用是在this 上进行的,根据定义它不为空)
  • @njzk2 我已经更新了问题
  • 实际错误在for(File child : children) {,表示null为children,表示dir不是目录,这种可能性不大。
  • @njzk2 那我应该怎么解决呢?有什么想法吗?
  • 再次,您的异常与代码不匹配。第 93 行和第 94 行应该是一个接一个,但在您的代码中有 if(child.isDirectory()) { 介于两者之间,这表明您实际上并未测试 isDirectory,这确实解释了崩溃。请发布一致的代码和堆栈跟踪。

标签: java android


【解决方案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;
    }
}

【讨论】:

  • 这段代码解决了没有文件时的问题,但有文件时却找不到文件...
  • 如果文件不在第一个目录中,这将不起作用,因为它无法测试第二个目录。
  • 好吧,我在 windows 中尝试了你的原始代码,我得到了 NullPointerException。然后,我尝试调试,看到程序尝试列出程序没有权限的文件夹的文件时会出现NullPointerException,File[] children = dir.listFiles(); return null 并使下一个 for 循环引发异常所以,让我更新答案,我的程序应该返回第一个找到的文件
  • new File("C:/") ?你看到android 标签了吗?
【解决方案2】:

感谢@njzk2 和@VinhNT 帮助我找到了解决方法,问题正如@VinhNT 所说,无法访问某些文件夹,所以我修改了代码使其不会陷入这样的困境案例,这是我的处理方式:

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;
}

希望它能帮助像我一样被卡住的其他人。

【讨论】:

  • 哈,我目前的电脑没有android sdk版本,我想只是改变应用程序为apk适配的路径,如果我错了告诉我
  • 实际上我想使用应用程序本身内部的按钮共享我的应用程序,所以我不确定用户将应用程序下载到其中的文件夹在哪里(下载、icm/bazaar 等)所以由于文件夹的变化,我需要让我的搜索遍历所有 sdcard,我不能只关注 sdcard 中的单个文件夹。
  • 我看到了这个stackoverflow.com/questions/7908193/…我不认为你必须搜索所有的sd卡来找到你的apk文件,只需在下载文件夹中找到就足够了
  • 不,用户可能只是从任何地方获取应用程序(可能在电脑上下载然后复制到 sd 卡)所以我需要全部检查
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
相关资源
最近更新 更多