KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > io > FileLister


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * foo.java
26  *
27  * Created on November 11, 2001, 12:09 AM
28  */

29
30 package com.sun.enterprise.util.io;
31 import java.io.*;
32 import java.util.*;
33
34 /**
35  *
36  * @author bnevins
37  * @version
38  */

39 public abstract class FileLister
40 {
41     FileLister(File root)
42     {
43         mainRoot = root;
44         fileList = new ArrayList();
45     }
46
47     abstract protected boolean relativePath();
48     
49     public String JavaDoc[] getFiles()
50     {
51         getFilesInternal(mainRoot);
52         String JavaDoc[] files = new String JavaDoc[fileList.size()];
53         
54         if(files.length <= 0)
55             return files;
56
57         int len = 0;
58         
59         if(relativePath())
60             len = mainRoot.getPath().length() + 1;
61         
62         for(int i = 0; i < files.length; i++)
63         {
64             files[i] = ((File)fileList.get(i)).getPath().substring(len).replace('\\', '/');
65         }
66         
67         Arrays.sort(files, String.CASE_INSENSITIVE_ORDER);
68         return files;
69     }
70     
71     
72     public void getFilesInternal(File root)
73     {
74         File[] files = root.listFiles();
75         
76         for(int i = 0; i < files.length; i++)
77         {
78             if(files[i].isDirectory())
79             {
80                 getFilesInternal(files[i]);
81             }
82             else
83                 fileList.add(files[i]); // actual file
84
}
85     }
86         
87         
88     
89
90
91
92
93     private ArrayList fileList = null;
94     private File mainRoot = null;
95 }
96
97
98
99
Popular Tags