1 17 18 package com.finalist.jag.util; 19 20 import java.io.*; 21 import java.util.*; 22 23 24 31 public class DirectoryIterator { 32 33 34 protected File rootDirectoryFile = null; 35 36 37 protected File currentDirectory = null; 38 39 40 protected boolean bRecursive = true; 41 42 43 protected boolean bIncludeDirs = false; 44 45 46 protected DirectoryIterator currInterator = null; 47 48 49 protected File[] currentFiles = null; 50 51 52 protected int nCurrentFileIndex = 0; 53 54 55 62 public DirectoryIterator(String sRootpath) { 63 this(sRootpath, true); 64 } 65 66 67 75 public DirectoryIterator(String sRootpath, boolean bRecursive) { 76 this(sRootpath, true, false); 77 } 78 79 80 public DirectoryIterator(String sRootpath, boolean bRecursive, boolean bIncludeDirs) { 81 rootDirectoryFile = new File(sRootpath); 82 this.bRecursive = bRecursive; 83 this.bIncludeDirs = bIncludeDirs; 84 } 85 86 87 94 public File getNext() { 95 96 if (currInterator != null) { 97 File file = null; 98 if ((file = currInterator.getNext()) != null) { 99 return file; 100 } 101 } 102 103 if ((currentFiles != null) && (nCurrentFileIndex < currentFiles.length)) { 104 File file = currentFiles[nCurrentFileIndex]; 105 if (file.isDirectory() && currentDirectory != file) { 106 currentDirectory = file; 107 return file; 108 } 109 nCurrentFileIndex++; 110 if (file.isDirectory()) { 111 if (file.compareTo(rootDirectoryFile) != 0) { 112 currInterator = new DirectoryIterator(file.getPath(), bRecursive); 113 } 114 return getNext(); 115 } 116 return file; 117 } 118 else if (currentFiles != null) { 119 return null; 120 } 121 currentFiles = rootDirectoryFile.listFiles(); 122 nCurrentFileIndex = 0; 123 124 return (currentFiles != null) ? getNext() : null; 125 } 126 } 127 128 ; 129 | Popular Tags |