KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > util > DirectoryIterator


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.util;
19
20 import java.io.*;
21 import java.util.*;
22
23
24 /**
25  * Class DirectoryIterator
26  *
27  *
28  * @author Wendel D. de Witte
29  * @version %I%, %G%
30  */

31 public class DirectoryIterator {
32
33    /** Field rootDirectoryFile */
34    protected File rootDirectoryFile = null;
35
36    /** Field currentDirecory */
37    protected File currentDirectory = null;
38
39    /** Field bRecursive */
40    protected boolean bRecursive = true;
41
42    /** Field bRecursive */
43    protected boolean bIncludeDirs = false;
44
45    /** Field currInterator */
46    protected DirectoryIterator currInterator = null;
47
48    /** Field currentFiles */
49    protected File[] currentFiles = null;
50
51    /** Field nCurrentFileIndex */
52    protected int nCurrentFileIndex = 0;
53
54
55    /**
56     * Constructor DirectoryIterator
57     *
58     *
59     * @param sRootpath
60     *
61     */

62    public DirectoryIterator(String JavaDoc sRootpath) {
63       this(sRootpath, true);
64    }
65
66
67    /**
68     * Constructor DirectoryIterator
69     *
70     *
71     * @param sRootpath
72     * @param bRecursive
73     *
74     */

75    public DirectoryIterator(String JavaDoc sRootpath, boolean bRecursive) {
76       this(sRootpath, true, false);
77    }
78
79
80    public DirectoryIterator(String JavaDoc sRootpath, boolean bRecursive, boolean bIncludeDirs) {
81       rootDirectoryFile = new File(sRootpath);
82       this.bRecursive = bRecursive;
83       this.bIncludeDirs = bIncludeDirs;
84    }
85
86
87    /**
88     * Method getNext
89     *
90     *
91     * @return
92     *
93     */

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