home *** CD-ROM | disk | FTP | other *** search
Java Source | 2009-04-18 | 1.9 KB | 95 lines |
- import java.io.*;
- import java.util.*;
-
- /**
- * ╛≥╖∩ñ╦ñóñªÑ╒ÑíÑñÑδñ╬ÑΩÑ╣Ñ╚ñ≥║ε└«íª╩▌╗²ñ╣ñδ├Ω╛▌Ñ»ÑΘÑ╣íú
- */
- public abstract class FileFinder {
- /**
- * ╖δ▓╠ñ╬Ñ╒ÑíÑñÑδñ╬ÑΩÑ╣Ñ╚
- */
- private ArrayList<File> result;
-
- /**
- * ╕½ñ─ñ▒ñ┐Ñ╒ÑíÑñÑδñ≥╬≤╡≤╖δ▓╠ñ╦┤▐ñßñδñ½╚╜─Ωñ╣ñδ├Ω╛▌ÑßÑ╜Ñ├Ñ╔íú
- * @return ╬≤╡≤ñ╣ñδñ╚ñ¡true
- */
- abstract boolean filter(File f);
-
- /**
- * Ñ╟ÑúÑ∞Ñ»Ñ╚ÑΩñ╬├╡║≈ñ≥┬╟ñ┴└┌ñδñ½╖ΦñßñδÑßÑ╜Ñ├Ñ╔íú
- * filter()ñ╬╖δ▓╠ñ╚ñ╧╞╚╬⌐ñ╦╞░║εñ╣ñδíú
- */
- abstract boolean prune(File f);
-
- //----------------------------------------------------------------------
-
- /**
- * Ñ│Ñ≤Ñ╣Ñ╚ÑΘÑ»Ñ┐
- */
- public FileFinder() {
- result = new ArrayList<File>();
- }
-
- /**
- * Ñ╚ÑΘÑ╨í╝Ñ╣ñ≥╝┬╗▄ñ╣ñδíú
- */
- protected void traverse(File f) {
- if(filter(f))
- result.add(f);
-
- if(f.isDirectory()) {
- if(prune(f))
- return; // ├╡║≈┬╟ñ┴└┌ñΩ
- File[] arr = f.listFiles();
- if(arr == null)
- return; // Ñ╟ÑúÑ∞Ñ»Ñ╚ÑΩñ╬├µ┐╚ñ¼╝Φ╞└ñ╟ñ¡ñ╩ññ
- for(int i = 0; i < arr.length; i++)
- traverse(arr[i]);
- }
- }
-
- /**
- * Ñ╚ÑΘÑ╨í╝Ñ╣ñ≥╝┬╗▄ñ╖ñ╞╖δ▓╠ñ≥╩▌┬╕ñ╖íó╝½╩¼╝½┐╚ñ≥╩╓ñ╣
- */
- public FileFinder find(File f) {
- traverse(f);
- return this;
- }
-
- /**
- * ╖δ▓╠ñ≥List<File>ñ╚ñ╖ñ╞╝ΦñΩ╜╨ñ╣
- */
- public List<File> getList() {
- return result;
- }
-
- /**
- * ╖δ▓╠ñ≥File[]ñ╚ñ╖ñ╞╝ΦñΩ╜╨ñ╣
- */
- public File[] getArray() {
- return result.toArray(new File[0]);
- }
-
- //----------------------------------------------------------------------
-
- /**
- * Ñ╞Ñ╣Ñ╚íúÑ∞Ñ«ÑσÑΘí╝Ñ╒ÑíÑñÑδñ≥ÑΩÑ╣Ñ╚ÑóÑ├Ñ╫ñ╣ñδ
- */
- public static void main(String argv[]) {
- FileFinder ff = new FileFinder() {
- boolean filter(File f) {
- return f.canRead();
- }
- boolean prune(File f) {
- return false;
- }
- };
- Iterator<File> i = ff.find(new File(argv[0])).getList().iterator();
- while(i.hasNext()) {
- File f = i.next();
- System.out.println(f.getPath());
- }
- }
- }
-