gpt4 book ai didi

java - 传递 Filefilter 作为参数时出现 IOException

转载 作者:行者123 更新时间:2023-12-02 00:35:13 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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 true数据库错误cdr文件长度:1有 1 个或多个文件

C:\Users\java>

关于java - 传递 Filefilter 作为参数时出现 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7995847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com