Help with String Arrays
807597Sep 19 2005 — edited Sep 21 2005Admittedly I am a rank beginner with Java. I want to know enough to be able to develop basic tools. I have "borrowed" and modified some code for searching and listing directories. I need help understanding how to select files based on part of the file name. I want two arrays, one containing all poll*.* files and the other fin.* files. The file extension will be three numeric digits (e.g. pollc1.123). Once in two arrays, I want to find cases where poll*.nnn (where nnn is a specific numeric) exists without an associated fin.nnn (e.g. pollc1.123 exists but fin.123 does not). Assuming that I can get to this point I have several operations to execute. Any help or advice will be appreciated and I will continue to study and search for these solutions as well. Thanks and here�s the current single-array code (2 classes in a single package).
DirList.java
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class DirList {
public static void main(String[] args) {
File path = new File("c:/jda/windss/poll");
String filetype = path.list.substring( 1, 4 );
String[] list;
if(args.length == 0)
list = path.list();
} else {
if(filetype == "poll")
list = path.list(new DirFilter(args[0]));
Arrays.sort(list, new AlphabeticComparator());
for(int i = 0; i < list.length; i++)
System.out.println(list);
}
}
}
class DirFilter implements FilenameFilter {
private Pattern pattern;
public DirFilter(String regex) {
pattern = Pattern.compile(regex);
}
public boolean accept(File dir, String name) {
// Strip path information:
return pattern.matcher(
new File(name).getName()).matches();
}
}
AlphabeticComparator,java
import java.util.*;
public class AlphabeticComparator implements Comparator{
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
return s1.toLowerCase().compareTo(
s2.toLowerCase());
}
}