advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 2.3/5 | Rate this item | 13 users have rated this item.
Expertise: Beginner
Language: Java
September 6, 2011
Java Code to Filter File Names in a Directory

The following Java code does the following:

  1. Filters files for an array of extensions.
  2. Splits file names.
  3. Tests the resulting array
package FileHandling;
import java.io.File;
import java.io.FilenameFilter;
/**
* filters files for an array of extensions critstr
* splitting file name by splitstr
* last element of resulting array being tested
* @author emniyet
*/
public final class MyFileFilter implements FilenameFilter {
private String [] critstr;
private String splitstr;
public void setCriteria(String splitstr, String [] critstr) {
this.splitstr=splitstr;
this.critstr=critstr;
}
public boolean accept(File dir, String name) {
boolean res=false;
String [] string = name.split(splitstr);
Y:
for (int j=0;j<critstr.length;j++){
if(string[string.length-1].equalsIgnoreCase(critstr[j])) {
res=true;
break Y;
}
}
return res;
}
}
code fragment using the FileNameFilter.
......
File fil = new File("C:\\"
+ "Users\\"
+ "emniyet\\"
+ "Documents\\"
+ "NetBeansProjects\\"
+ "JSaat\\"
+ "src\\"
+ "JSaatSaat\\");
MyFileFilter flt = new MyFileFilter();
String [] crit= {"wav", "au", "mid"};
flt.setCriteria("\\.", crit);
this.adclip = fil.list(flt);
for(int j=0;j
deniz mermerci
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement