KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > File


1 /*
2   Copyright (C) 2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program 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 Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.io.FilenameFilter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24
25 /**
26  * Provides recursive file listing, and replaces leading ~ by the
27  * user's home directory.
28  */

29 public class File extends java.io.File JavaDoc {
30     public File(java.io.File JavaDoc file) {
31         this(file.getPath());
32     }
33     public File(String JavaDoc pathname) {
34         super(Files.expandFileName(pathname));
35     }
36     public File(File parent, String JavaDoc child) {
37         this(parent.getPath(),child);
38     }
39     public File(java.io.File JavaDoc parent, String JavaDoc child) {
40         this(parent.getPath(),child);
41     }
42     public File(String JavaDoc parent, String JavaDoc child) {
43         super(Files.expandFileName(parent),child);
44     }
45
46     /**
47      * Recursively list files matching a filter
48      * @param filter list files matching this filter
49      * @return a List of File matching the filter
50      * @see #listFilesRecursively(FilenameFilter,List)
51      */

52     public List JavaDoc listFilesRecursively(FilenameFilter JavaDoc filter) {
53         LinkedList JavaDoc files = new LinkedList JavaDoc();
54         listFilesRecursively(filter,files);
55         return files;
56     }
57
58     /**
59      * Recursively list files matching a filter
60      * @param filter list files matching this filter
61      * @param files add matching files to this list
62      * @see #listFilesRecursively(FilenameFilter)
63      */

64     public void listFilesRecursively(FilenameFilter JavaDoc filter, List JavaDoc files) {
65         String JavaDoc[] names = list();
66         if(names==null) return;
67         for (int i=0; i<names.length; i++) {
68             File file = new File(this,names[i]);
69             if (filter.accept(this,names[i]))
70                 files.add(file);
71             if (file.isDirectory())
72                 file.listFilesRecursively(filter,files);
73         }
74     }
75
76     /**
77      * Gets a path relative to a parent directory of the file.
78      *
79      * @param parent the directory to give a path relative to
80      * @return a path relative to parent, or getPath() if parent is
81      * not a parent of the file.
82      */

83     public String JavaDoc getRelativePath(File parent) throws IOException JavaDoc {
84         String JavaDoc parentPath = parent.getCanonicalPath();
85         String JavaDoc path = getCanonicalPath();
86         if (path.startsWith(parentPath)) {
87             return path.substring(parentPath.length()+1);
88         } else {
89             return getPath();
90         }
91     }
92
93     public java.io.File JavaDoc[] listDirectories() {
94         return listFiles(Files.directoryFilter);
95     }
96
97     public java.io.File JavaDoc[] listNonHiddenFiles() {
98         return listFiles(Files.nonHiddenFilter);
99     }
100 }
101
102
Popular Tags