📄 findfile.java
字号:
package jodd.file;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import jodd.util.StringFlags;
/**
* Generic file finder, browses all files of specified folder. Search starts
* with the call to <code>first()</code> method, that will return founded
* file or <code>null</code> if no file found. Method <code>next()</code>
* then retrieves all other founded files, one by one. It will return
* <code>null</code> when search is over. Here is the example:
* <code>
* FindFile ff = new FindFile();
* File f = ff.first("class");
* while (f != null) {
*
* f = ff.next();
* }
* </code>
*
* This is default behaviour, and it can be changed with following
* flags:
* <ul>
* <li>r - search subdirectories (i.e. recursivly)</li>
* <li>d - also include directories in search results</li>
* <li>x - exclude files from search results</li>
* </ul>
*
* A list of <code>FileFilter</code> implementation can be added to the
* search. Each file is matched on every availiable filter, and only if
* all filters accept the file, it will be returned.
*
* @see java.io.FileFilter
*/
public class FindFile {
// ---------------------------------------------------------------- flags
private boolean recursive = false;
/**
* Sets recursve flag.
*
* @param v
*/
public void setRecursive(boolean v) {
recursive = v;
}
/**
* Returns recursive flag.
*
* @return recursive flag
*/
public boolean getRecursive() {
return recursive;
}
private boolean incDirs;
/**
* Sets include dir flag.
*
* @param v
*/
public void setIncludeDirs(boolean v) {
incDirs = v;
}
/**
* Returns include dir flag.
*/
public boolean getIncludeDirs() {
return incDirs;
}
private boolean exFiles;
/**
* Sets exclude dir flag.
*
* @param v
*/
public void setExcludeFiles(boolean v) {
exFiles = v;
}
/**
* Returns exclude dir flag.
*/
public boolean getExcludeFiles() {
return exFiles;
}
private static StringFlags flags = new StringFlags();
static {
flags.addFlag('r', "recursive", true);
flags.addFlag('d', "includeDirs", true);
flags.addFlag('x', "excludeFiles", true);
}
// ---------------------------------------------------------------- filters
private List ffilters = null;
private int ffilters_size = 0;
public void setFileFilters(List ffs) {
ffilters = ffs;
if (ffilters != null) {
ffilters_size = ffilters.size();
} else {
ffilters_size = 0;
}
}
public void setFileFilters(FileFilter[] ff) {
ArrayList al = new ArrayList(ff.length);
for (int i = 0; i < ff.length; i++) {
al.add(ff[i]);
}
setFileFilters(al);
}
public void setFileFilters(FileFilter ff) {
ArrayList al = new ArrayList();
al.add(ff);
setFileFilters(al);
}
public void setFileFilters() {
setFileFilters((List) null);
}
public List getFileFilters() {
return ffilters;
}
public FileFilter getFileFilter() {
if (ffilters != null) {
return (FileFilter) ffilters.get(0);
} else {
return null;
}
}
public void addFileFilter(FileFilter ff) {
if (ffilters == null) {
ffilters = new ArrayList();
}
ffilters.add(ff);
}
// ---------------------------------------------------------------- finder
private ArrayList fileList = null;
private int ndx = 0;
private int fileListSize = 0;
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
* @param opt options
* @param ff file filter to use
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir, String opt, FileFilter ff) {
setFileFilters(ff);
return dofirst(dir, opt);
}
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
* @param ff file filter to use
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir, FileFilter ff) {
setFileFilters(ff);
return dofirst(dir, "");
}
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
* @param opt options
* @param ffa list of file filters to use
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir, String opt, FileFilter[] ffa) {
setFileFilters(ffa);
return dofirst(dir, opt);
}
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
* @param ffa list of file filters to use
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir, FileFilter[] ffa) {
setFileFilters(ffa);
return dofirst(dir, "");
}
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
* @param opt options
* @param ffs file filters list
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir, String opt, List ffs) {
setFileFilters(ffs);
return dofirst(dir, opt);
}
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
* @param ffs file filters list
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir, List ffs) {
setFileFilters(ffs);
return dofirst(dir, "");
}
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
* @param opt options
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir, String opt) {
setFileFilters();
return dofirst(dir, opt);
}
/**
* Starts finding process and returns very first file.
*
* @param dir starting folder
*
* @return founded file, or <code>null</code> if no file found
*/
public File first(String dir) {
return dofirst(dir, "");
}
/**
* Private method that starts findings
*
* @param dir starting folder
* @param opt options
*
* @return founded file
*/
private File dofirst(String dir, String opt) {
flags.parse(opt, this);
// start
fileList = new ArrayList();
File f = new File(dir);
if (f.exists() == false) {
return null; // directory (or file) doesn't exist
}
if (f.isDirectory() == false) {
return f; // if not a directory, return it
}
File[] childs = f.listFiles();
for (int i = 0; i < childs.length; i++) {
fileList.add(childs[i]);
}
ndx = 0;
fileListSize = fileList.size();
return next();
}
/**
* Finds the next file once when search is activated.
*
* @return founded file, <code>null</code> if no file has been found or if no more files.
*/
public File next() {
if (ndx == fileListSize) {
return end();
}
File f = null;
boolean found = false;
loop: while (ndx < fileListSize) {
f = (File) fileList.get(ndx);
ndx++;
if (f.isDirectory()) { // directory found
fileList.subList(0, ndx).clear(); // release previous list elements from memory
ndx = 0;
if (recursive == true) { // recursive: append subfolder files to the list
File[] childs = f.listFiles();
for (int i = 0; i < childs.length; i++) {
fileList.add(childs[i]);
}
}
fileListSize = fileList.size();
if (incDirs == false) { // exclude dirs
continue;
}
}
if (f.isFile()) { // exclude files
if (exFiles == true) {
continue;
}
}
if (ffilters == null) {
found = true;
break;
}
for (int i = 0; i < ffilters_size; i++) {
FileFilter ff = (FileFilter) ffilters.get(i);
if (ff.accept(f) == true) {
found = true;
break loop;
}
}
}
if (found == false) {
return end();
}
return f;
}
/**
* Finishes the search and frees resources.
*
* @return always <code>null</code>: it means the search is over.
*/
private File end() {
fileList = null;
ffilters = null;
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -