传递 Filefilter 作为参数时出现 IOException
当我执行这段代码时,我遇到了这个奇怪的问题:
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.");
}
}
}
I have this strange problem when I execute this piece of code:
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");
}
The directory here contains 1 file that matches the filter.
The output is:
cdrFiles length: 0
java.io.IOException: 1No files in dir: cdrs that match the correct pattern
When I comment the exception:
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");
}
I get this output:
cdrFiles length: 1
there is 1 or more files
Does anyone know how this is possible?
EDIT:
This is the code for the filter:
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"));
}
});
With the first code the filename isn't printed.
With the second code it is (even checked if it matched -> yes)
File in directory:
call_history_20111001_20111031_465_answer.csv
collectCdrFiles function look like this:
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;
}
same problem with this code:
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.");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答
我解决了问题。不知道为什么,但是当我将过滤器作为参数传递时,它失败了。所有其他方式(内联或单独的类)都使用相同的代码,
以便仍然能够通过它,我是这样做的:
在函数中我实例化了该类:
丑陋如地狱,但它可以工作^^
谢谢帮助伙计们
ANSWER
I fixed the problem. Don't know why, but when i pass the filter as argument it fails. All other ways (in-line or separate class) work with identical code
so to still be able to pass it i did it like this:
and in the function i instanciate the class:
ugly as hell but it works ^^
thx for the help guys
我在我的机器上复制了你的代码。它对我来说效果很好。我使用的过滤器与确切的名称匹配,而不是正则表达式模式。我这样做只是为了测试其余的代码。
}
}
这是输出:
C:\Users\java>java filetest
call_history_20111001_20111031_465_answer.csv true
数据库错误
cdr文件长度:1
有 1 个或多个文件
C:\Users\java>
i replicated your code on my machine. It works fine for me. The filter i was using matches the exact name, not a regex pattern. I did this just so i can test the rest of the code.
}
}
}
Here is the output:
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:\Users\java>
针对与 Weblogic 10.3.4 输出捆绑在一起的 JRockit 进行编译
:
Compiled against JRockit that is bundled with Weblogic 10.3.4
ouput: