Reputation: 1178
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.");
}
}
}
Upvotes: 0
Views: 608
Reputation: 1178
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:
cdrFiles = collectCdrFiles(directory, new CdrFilenameFilterUnifiedTelecom().getClass());
and in the function i instanciate the class:
cdrFiles = dir.list((FilenameFilter)filter.newInstance());
ugly as hell but it works ^^
thx for the help guys
Upvotes: 1
Reputation: 24336
Compiled against JRockit that is bundled with Weblogic 10.3.4
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;
}
}
ouput:
call_history_20111001_20111031_465_answer.csv true
cdrFiles length: 1
there is 1 or more files
Upvotes: 1
Reputation: 4324
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.
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;
} }
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>
Upvotes: 1