Android - string.equals()无法正常工作

时间:2015-04-13 23:50:38

标签: android

所以我因为一些奇怪的原因而遇到了.equals的问题,我过去曾经使用过它,直到这一次都很好用。所以基本上我的方法是提取一个zip文件,扫描文件夹中的特定文件夹名称。看看它的样子:

 public void scanFolder(File src){
        File[] files = src.listFiles(new FileFilter(){

            @Override
            public boolean accept(File pathname) {
                if(pathname.isDirectory()){
                    String name = new String(pathname.getName());
                    Toast.makeText(AndroidFileBrowserExampleActivity.this,  
                      name, Toast.LENGTH_SHORT).show();
                    if(name.equals("assets")){ //problem is here
                        String path =  
                         pathname.toString().replace("assets",  ""); 
                        File src = new File(path);
                        File dest = new File("/sdcard/.MCPE Texture    
                           Installer/0.10.5/");
                        try {
                            zip(src, dest);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }else{
                        scanFolder(pathname);
                    }
                }
                return true;
            }

        });
    }

因此,即使文件夹名称是资产,当我使用name.equals(assets)时,它永远不会匹配并继续循环。但有什么奇怪的是我用另一种方法测试了这个并且我手动在设备上创建了文件并且它工作正常,这是方法:

public void scanFolder(File src){
        File[] files = src.listFiles(new FileFilter(){

            @Override
            public boolean accept(File pathname) {
                if(pathname.isDirectory()){
                    String name = pathname.getName();
                    if(name.equals("assets")){
                        String path = pathname.toString().replace("assets", 
""); 
                        Toast.makeText(MainActivity.this, path, 
Toast.LENGTH_LONG).show();
                    }else{
                        scanFolder(pathname);
                    }
                }
                return pathname.isDirectory();
            }

        });
    }

我对此非常困惑,所以如果有人能告诉我我做错了什么就会很棒!谢谢!

编辑: 下面是与scanFolder方法有关的调试日志:

04-13 19:20:56.113: W/System.err(11052): java.io.FileNotFoundException: /sdcard/.MCPE Texture Installer/0.10.5: open failed: EISDIR (Is a directory)
04-13 19:20:56.113: W/System.err(11052):    at libcore.io.IoBridge.open(IoBridge.java:409)
04-13 19:20:56.113: W/System.err(11052):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
04-13 19:20:56.113: W/System.err(11052):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity.zip(AndroidFileBrowserExampleActivity.java:546)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity$1.accept(AndroidFileBrowserExampleActivity.java:629)
04-13 19:20:56.113: W/System.err(11052):    at java.io.File.listFiles(File.java:830)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity.scanFolder(AndroidFileBrowserExampleActivity.java:617)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity$1.accept(AndroidFileBrowserExampleActivity.java:635)
04-13 19:20:56.113: W/System.err(11052):    at java.io.File.listFiles(File.java:830)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity.scanFolder(AndroidFileBrowserExampleActivity.java:617)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity$1.accept(AndroidFileBrowserExampleActivity.java:635)
04-13 19:20:56.113: W/System.err(11052):    at java.io.File.listFiles(File.java:830)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity.scanFolder(AndroidFileBrowserExampleActivity.java:617)
04-13 19:20:56.113: W/System.err(11052):    at com.dogger20011.mcpetextureinstaller.AndroidFileBrowserExampleActivity.onActivityResult(AndroidFileBrowserExampleActivity.java:150)
04-13 19:20:56.113: W/System.err(11052):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
04-13 19:20:56.113: W/System.err(11052):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
04-13 19:20:56.113: W/System.err(11052):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
04-13 19:20:56.113: W/System.err(11052):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
04-13 19:20:56.113: W/System.err(11052):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
04-13 19:20:56.113: W/System.err(11052):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 19:20:56.113: W/System.err(11052):    at android.os.Looper.loop(Looper.java:136)
04-13 19:20:56.113: W/System.err(11052):    at android.app.ActivityThread.main(ActivityThread.java:5021)
04-13 19:20:56.113: W/System.err(11052):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 19:20:56.113: W/System.err(11052):    at java.lang.reflect.Method.invoke(Method.java:515)
04-13 19:20:56.113: W/System.err(11052):    at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:827)
04-13 19:20:56.113: W/System.err(11052):    at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-13 19:20:56.113: W/System.err(11052):    at 
dalvik.system.NativeStart.main(Native Method)
04-13 19:20:56.113: W/System.err(11052): Caused by: 
libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
04-13 19:20:56.113: W/System.err(11052):    at libcore.io.Posix.open(Native 
Method)
04-13 19:20:56.113: W/System.err(11052):    at  
libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-13 19:20:56.113: W/System.err(11052):    at 
libcore.io.IoBridge.open(IoBridge.java:393)
04-13 19:20:56.113: W/System.err(11052):    ... 26 more

0 个答案:

没有答案