【问题标题】:IOException when passing Filefilter as argument将 Filefilter 作为参数传递时出现 IOException
【发布时间】:2011-11-03 13:37:10
【问题描述】:

我在执行这段代码时遇到了这个奇怪的问题:

cdrFiles = dir.list(filter);
System.out.println("cdrFiles length: " + cdrFiles.length);
if (cdrFiles.length >= 1) {
    System.out.println("there is 1 or more files");
}else{
    throw new IOException("1No files in dir: " + directory + " that match the correct pattern");
}

此处的目录包含 1 个与过滤器匹配的文件。

输出是:

cdrFiles length: 0
java.io.IOException: 1No files in dir: cdrs that match the correct pattern

当我评论异常时:

cdrFiles = dir.list(filter);
System.out.println("cdrFiles length: " + cdrFiles.length);
if (cdrFiles.length >= 1) {
    System.out.println("there is 1 or more files");
}else{
    //throw new IOException("1No files in dir: " + directory + " that match the correct pattern");
}

我得到这个输出:

cdrFiles length: 1
there is 1 or more files

有人知道这怎么可能吗?

编辑:

这是过滤器的代码:

String[] cdrFiles = collectCdrFiles(directory, new FilenameFilter() {

    public boolean accept(File dir, String name) {
        System.out.println(name + "\t" + name.matches("call_history_[0-9]{8}_[0-9]{8}_[0-9]{3}_answer.*\.csv"));
        return name.matches("call_history_[0-9]{8}_[0-9]{8}_[0-9]{3}_answer.*\.csv"));
    }
});

第一个代码不会打印文件名。
使用第二个代码(甚至检查它是否匹配 -> 是)

目录中的文件:

call_history_20111001_20111031_465_answer.csv

collectCdrFiles 函数如下所示:

protected String[] collectCdrFiles(String directory, FilenameFilter filter) throws IOException {
    //open cdr directory
    String[] cdrFiles;
    File dir = new File(directory);
    //get cdr files
    if (dir.exists()) {
        cdrFiles = dir.list(filter);
        System.out.println("cdrFiles length: " + cdrFiles.length);

        if (cdrFiles.length >= 1) {
            System.out.println("there is 1 or more files");
        }else{
            throw new IOException("1No files in dir: " + directory + " that match the correct pattern");
        }
    } else {
        throw new IOException("Directory: " + directory + " doesn't exist.");
    }
    return cdrFiles;
}

这段代码有同样的问题:

if (cdrFiles.length >= 1) {
    System.out.println("there is 1 or more files");
}
if(cdrFiles.length == 0){    
    throw new IOException("1No files in dir: " + directory + " that match the correct pattern");
}

SSCCEE:

public static void main(String[] args) throws IOException {
    String[] cdrFiles;
    File dir = new File("testfolder");
    //get cdr files
    if (dir.exists()) {
        cdrFiles = dir.list(new FilenameFilter() {

        public boolean accept(File dir, String name) {
        System.out.println(name + "\t" + name.matches("call_history_[0-9]{8}_[0-9]{8}_[0-9]{3}_answer.*\\.csv"));
        return name.matches("call_history_[0-9]{8}_[0-9]{8}_[0-9]{3}_answer.*\\.csv");
        }
    });
        System.out.println("cdrFiles length: " + cdrFiles.length);

        if (cdrFiles.length >= 1) {
        System.out.println("there is 1 or more files");
        }
        if(cdrFiles.length == 0){        
        throw new IOException("1No files in dir: " + dir.getName() + " that match the correct pattern");
        }
    } else {
        throw new IOException("Directory: " + dir.getName() + " doesn't exist.");
    }
    }
}

【问题讨论】:

  • 不,这种变化不会产生这种差异,除非有一些递归调用正在进行。请发布更多代码。例如,filter 背后的值/代码是什么?
  • 仍然怀疑其他地方发生了变化。当您重新启用异常时,clean 您的工作区(即删除所有 .class 文件)并进行完全重建,输出是否仍为 0?
  • @Berty 请张贴您的main 方法
  • @Joachim 我已经尝试过干净的构建,仍然是 0
  • @Woot4Moo 应用程序有一个 gui。 main方法比较远,中间有几百行代码

标签: java exception ioexception filefilter


【解决方案1】:

我在我的机器上复制了您的代码。这对我来说可以。我使用的过滤器与确切名称匹配,而不是正则表达式模式。我这样做只是为了测试其余的代码。

import java.io.*;

public class filetest {

public static void main(String [] m) throws IOException {

String directory = "c://dev//";

filetest t = new filetest();
String[] cdrFiles = t.collectCdrFiles(directory, new FilenameFilter() {

    public boolean accept(File dir, String name) {
        System.out.println(name + "\t" +       name.matches("call_history_20111001_20111031_465_answer.csv"));
        return name.matches("call_history_20111001_20111031_465_answer.csv");
    }
});

}

protected String[] collectCdrFiles(String directory, FilenameFilter filter) throws     IOException {
  //open cdr directory
   String[] cdrFiles;
  File dir = new File(directory);
   //get cdr files
   if (dir.exists()) {
       cdrFiles = dir.list(filter);
       System.out.println("cdrFiles length: " + cdrFiles.length);

       if (cdrFiles.length >= 1) {
           System.out.println("there is 1 or more files");
       }else{
           throw new IOException("1No files in dir: " + directory + " that match  the    correct pattern");
      }
   } else {
    throw new IOException("Directory: " + directory + " doesn't exist.");
   }
  return cdrFiles;

} }

这是输出:

C:\Users\java>java 文件测试 call_history_20111001_20111031_465_answer.csv 真 数据库错误 cdrFiles 长度:1 有 1 个或多个文件

C:\Users\java>

【讨论】:

    【解决方案2】:

    针对与 Weblogic 10.3.4 捆绑的 JRockit 编译

    import java.io.File;
    import java.io.FilenameFilter;
    import java.io.IOException;
    
    public class Test {
    
        public static void main(String[] args) throws IOException {
    
            String directory = "C:\\Test";
            String[] cdrFiles = collectCdrFiles(directory, new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    System.out
                            .println(name
                                    + "\t"
                                    + name
                                            .matches("call_history_[0-9]{8}_[0-9]{8}_[0-9]{3}_answer.*\\.csv"));
                    return name
                            .matches("call_history_[0-9]{8}_[0-9]{8}_[0-9]{3}_answer.*\\.csv");
                }
            });
        }
    
        static protected String[] collectCdrFiles(String directory,
                FilenameFilter filter) throws IOException { // open cdr directory
            String[] cdrFiles;
            File dir = new File(directory); // get cdr files
            if (dir.exists()) {
                cdrFiles = dir.list(filter);
                System.out.println("cdrFiles length: " + cdrFiles.length);
                if (cdrFiles.length >= 1) {
                    System.out.println("there is 1 or more files");
                } else {
                    throw new IOException("1No files in dir: " + directory
                            + " that match the correct pattern");
                }
            } else {
                throw new IOException("Directory: " + directory + " doesn't exist.");
            }
            return cdrFiles;
        }
    }
    

    输出:

    call_history_20111001_20111031_465_answer.csv   true
    cdrFiles length: 1
    there is 1 or more files
    

    【讨论】:

      【解决方案3】:

      回答

      我解决了这个问题。不知道为什么,但是当我将过滤器作为参数传递时,它失败了。所有其他方式(内联或单独的类)都使用相同的代码

      为了仍然能够通过它,我这样做了:

      cdrFiles = collectCdrFiles(directory, new CdrFilenameFilterUnifiedTelecom().getClass());
      

      并在函数中实例化类:

      cdrFiles = dir.list((FilenameFilter)filter.newInstance());
      

      丑得要命,但它有效^^

      感谢大家的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-21
        • 1970-01-01
        • 1970-01-01
        • 2021-02-10
        相关资源
        最近更新 更多