0

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

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.");
    }
    }
}
4

3 回答 3

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 filetest call_history_20111001_20111031_465_answer.csv true DB false cdrFiles length: 1 there is 1 or more files

C:\用户\java>

于 2011-11-03T14:07:45.393 回答
1

针对与 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
于 2011-11-03T14:09:47.943 回答
1

回答

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

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

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

在函数中我实例化了这个类:

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

丑得要命,但它有效^^

谢谢大家的帮助

于 2011-11-20T10:40:02.073 回答