How can I create a File[] for all *.properties under a given directory?
I have a directory, C:\myDir
.
under this directory, there may be folders and various files
How can I "loop" through this directory and build up a File array of all files with a .properties
extension?
I want to do some processing on these files, the reason for a File[] is to keep it dynamic (so additional properties files added in the future will be included)
I'm hoping to do something like this :
public static void main (String[] vargs)
{
// find all .proper开发者_开发问答ties files
//
//loop through property file and process
{
doSomething(myFile[i]);
}
}
public void doSomething(File myfile) {}
Look at FilenameFilter.
final FilenameFilter filter = new FilenameFilter() {
public boolean accept(final File directory, final String name) {
return name.endsWith(".properties");
}
};
You can use Apache Commons IO FileUtils class for it.
FileUtils.listFiles(File directory, String[] extensions, boolean recursive);
Hope this helps.
Using commons-io FilenameUtils
final File dir = new File("c:\\myDir");
final File[] files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(final File dir, final String name) {
return FilenameUtils.isExtension(name, "properties");
}
});
Implemented code which returns files recursing into directories.
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FileList {
public static void main(String[] args) {
File dir = new File("/usr/local/work_apps");
String filterExt = "properties";
List<File> fileList = new ArrayList<File>();
getFileteredFiles(dir,filterExt,fileList);
for (File file : fileList) {
System.out.println("FileList.main() "+file.getName());
}
}
/**
* Get files recursively filtered by file extention.
* @param dir search directory for.
* @param filterExt file extention filter.
* @param fileList List passed to method to be filled with file's.
*/
public static void getFileteredFiles(File dir,String filterExt,List<File> fileList){
if(dir.isDirectory()){
File[] files = dir.listFiles();
if(files != null && files.length > 0){
for (File file : files) {
if(file.isDirectory()){
getFileteredFiles(file, filterExt, fileList);
}else{
if(file.getName().endsWith("."+filterExt)){
fileList.add(file);
}
}
}
}
}
}
}
Here is a complete answer without external libs:
public class FileFinder{
public static List<File> findFiles(final File baseDir, final String regex){
final List<File> files = new ArrayList<File>();
scanFolder(baseDir, Pattern.compile(regex), files);
return files;
}
private static void scanFolder(final File dir,
final Pattern pattern,
final List<File> files){
for(final File candidate : dir.listFiles()){
if(candidate.isDirectory()){
scanFolder(candidate, pattern, files);
} else if(pattern.matcher(candidate.getName()).matches()){
files.add(candidate);
}
}
}
}
Call it like this:
public static void main(String[] args){
List<File> filesAsList = FileFinder.findFiles(
new File("c:\\my\\dir"), ".*\\.properties");
File[] filesAsArray = filesAsList.toArray(new File[filesAsList.size()]);
}
Apache commons IO has a nice class for traversing file systems called DirectoryWalker. If you want to find these properties files recursively, check it out.
For example, the below code will recursively find image files under a directory and return them as a List.
import com.google.common.collect.Lists;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.IOCase;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.HiddenFileFilter;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.io.filefilter.SuffixFileFilter;
class SourceImageFinder extends DirectoryWalker {
public SourceImageFinder() {
// visible directories and image files
super(new AndFileFilter(notHiddenFileFilter(), new OrFileFilter(imageFileFilter(), DirectoryFileFilter.INSTANCE)), -1);
}
public List<File> findSourceImages(File directory) {
List<File> results = Lists.newArrayList();
try {
walk(directory, results);
} catch (IOException ex) {
throw new RuntimeException("Problem finding images", ex);
}
return results;
}
@Override
protected void handleFile(File file, int depth, Collection results) throws IOException {
results.add(file);
}
private static IOFileFilter notHiddenFileFilter() {
// HiddenFileFilter.HIDDEN doesn't exclude files starting with '.' on Windows but I want to.
return new NotFileFilter(new OrFileFilter(HiddenFileFilter.HIDDEN, new PrefixFileFilter(".")));
}
private static IOFileFilter imageFileFilter() {
return new SuffixFileFilter(new String[]{"png", "jpg", "jpeg", "gif", "bmp", "tif", "tiff"}, IOCase.INSENSITIVE);
}
}
精彩评论