home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 4427 / aisp_memo-20090510.7z / hed_dat3_src / src / FileFinder.java < prev    next >
Encoding:
Java Source  |  2009-04-18  |  1.9 KB  |  95 lines

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  * ╛≥╖∩ñ╦ñóñªÑ╒ÑíÑñÑδñ╬ÑΩÑ╣Ñ╚ñ≥║ε└«íª╩▌╗²ñ╣ñδ├Ω╛▌Ñ»ÑΘÑ╣íú
  6.  */
  7. public abstract class FileFinder {
  8.     /**
  9.      * ╖δ▓╠ñ╬Ñ╒ÑíÑñÑδñ╬ÑΩÑ╣Ñ╚
  10.      */
  11.     private ArrayList<File> result;
  12.  
  13.     /**
  14.      * ╕½ñ─ñ▒ñ┐Ñ╒ÑíÑñÑδñ≥╬≤╡≤╖δ▓╠ñ╦┤▐ñßñδñ½╚╜─Ωñ╣ñδ├Ω╛▌ÑßÑ╜Ñ├Ñ╔íú
  15.      * @return    ╬≤╡≤ñ╣ñδñ╚ñ¡true
  16.      */
  17.     abstract boolean filter(File f);
  18.  
  19.     /**
  20.      * Ñ╟ÑúÑ∞Ñ»Ñ╚ÑΩñ╬├╡║≈ñ≥┬╟ñ┴└┌ñδñ½╖ΦñßñδÑßÑ╜Ñ├Ñ╔íú
  21.      * filter()ñ╬╖δ▓╠ñ╚ñ╧╞╚╬⌐ñ╦╞░║εñ╣ñδíú
  22.      */
  23.     abstract boolean prune(File f);
  24.  
  25.     //----------------------------------------------------------------------
  26.  
  27.     /**
  28.      * Ñ│Ñ≤Ñ╣Ñ╚ÑΘÑ»Ñ┐
  29.      */
  30.     public FileFinder() {
  31.     result = new ArrayList<File>();
  32.     }
  33.  
  34.     /**
  35.      * Ñ╚ÑΘÑ╨í╝Ñ╣ñ≥╝┬╗▄ñ╣ñδíú
  36.      */
  37.     protected void traverse(File f) {
  38.     if(filter(f))
  39.         result.add(f);
  40.  
  41.     if(f.isDirectory()) {
  42.         if(prune(f))
  43.         return; // ├╡║≈┬╟ñ┴└┌ñΩ
  44.         File[] arr = f.listFiles();
  45.         if(arr == null)
  46.         return; // Ñ╟ÑúÑ∞Ñ»Ñ╚ÑΩñ╬├µ┐╚ñ¼╝Φ╞└ñ╟ñ¡ñ╩ññ
  47.         for(int i = 0; i < arr.length; i++)
  48.         traverse(arr[i]);
  49.     }
  50.     }
  51.  
  52.     /**
  53.      * Ñ╚ÑΘÑ╨í╝Ñ╣ñ≥╝┬╗▄ñ╖ñ╞╖δ▓╠ñ≥╩▌┬╕ñ╖íó╝½╩¼╝½┐╚ñ≥╩╓ñ╣
  54.      */
  55.     public FileFinder find(File f) {
  56.     traverse(f);
  57.     return this;
  58.     }
  59.  
  60.     /**
  61.      * ╖δ▓╠ñ≥List<File>ñ╚ñ╖ñ╞╝ΦñΩ╜╨ñ╣
  62.      */
  63.     public List<File> getList() {
  64.     return result;
  65.     }
  66.  
  67.     /**
  68.      * ╖δ▓╠ñ≥File[]ñ╚ñ╖ñ╞╝ΦñΩ╜╨ñ╣
  69.      */
  70.     public File[] getArray() {
  71.     return result.toArray(new File[0]);
  72.     }
  73.  
  74.     //----------------------------------------------------------------------
  75.  
  76.     /**
  77.      * Ñ╞Ñ╣Ñ╚íúÑ∞Ñ«ÑσÑΘí╝Ñ╒ÑíÑñÑδñ≥ÑΩÑ╣Ñ╚ÑóÑ├Ñ╫ñ╣ñδ
  78.      */
  79.     public static void main(String argv[]) {
  80.     FileFinder ff = new FileFinder() {
  81.         boolean filter(File f) {
  82.             return f.canRead();
  83.         }
  84.         boolean prune(File f) {
  85.             return false;
  86.         }
  87.         };
  88.     Iterator<File> i = ff.find(new File(argv[0])).getList().iterator();
  89.     while(i.hasNext()) {
  90.         File f = i.next();
  91.         System.out.println(f.getPath());
  92.     }
  93.     }
  94. }
  95.