KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > util > Files


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.ioc.util;
8
9 import java.io.BufferedInputStream JavaDoc;
10 import java.io.BufferedOutputStream JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileFilter JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
26  */

27
28 public class Files {
29
30     /**
31      * <p/>
32      * Delete a file. If file is a directory, delete it and all sub-directories.
33      * </p>
34      * <p/>
35      * The difference between File.delete() and this method are:
36      * </p>
37      * <ul>
38      * <li>A directory to be deleted does not have to be empty.</li>
39      * <li>You get exceptions when a file or directory cannot be deleted.
40      * (java.io.File methods returns a boolean)</li>
41      * </ul>
42      *
43      * @param file file or directory to delete.
44      */

45     public static boolean delete(File JavaDoc file) {
46         if(!file.exists()) {
47             return true;
48         }
49         if(file.isDirectory()) {
50             return deleteDirectory(file);
51         }
52         else {
53             return file.delete();
54         }
55     }
56
57     /**
58      * Delete a file, or a directory and all of its contents.
59      *
60      * @param dir The directory or file to delete.
61      * @return True if all delete operations were successfull.
62      */

63     public static boolean deleteDirectory(File JavaDoc dir) {
64
65         if(!dir.exists()) return true;
66         boolean result = cleanDirectory(dir);
67         return result & dir.delete();
68     }
69
70     /**
71      * Clean a directory without deleting it.
72      *
73      * @param directory directory to clean
74      */

75     public static boolean cleanDirectory(File JavaDoc directory) {
76         if(!directory.exists()) {
77             return true;
78         }
79         if(!directory.isDirectory()) {
80             return false;
81         }
82         boolean result = true;
83
84         File JavaDoc[] files = directory.listFiles();
85         for(int i = 0; i < files.length; i++) {
86             File JavaDoc file = files[i];
87             result = result && delete(file);
88         }
89         return result;
90     }
91
92     /**
93      * Schedule a file to be deleted when JVM exits.
94      * If file is directory delete it and all sub-directories.
95      *
96      * @param file file or directory to delete.
97      * @throws IOException in case deletion is unsuccessful
98      */

99     public static void deleteOnExit(File JavaDoc file) throws IOException JavaDoc {
100         if(file.isDirectory()) {
101             deleteDirectoryOnExit(file);
102         }
103         else {
104             file.deleteOnExit();
105         }
106     }
107
108     /**
109      * Recursively schedule directory for deletion on JVM exit.
110      *
111      * @param directory directory to delete.
112      * @throws IOException in case deletion is unsuccessful
113      */

114     private static void deleteDirectoryOnExit(File JavaDoc directory)
115             throws IOException JavaDoc {
116         if(!directory.exists()) {
117             return;
118         }
119
120         cleanDirectoryOnExit(directory);
121         directory.deleteOnExit();
122     }
123
124     /**
125      * Clean a directory without deleting it.
126      *
127      * @param directory directory to clean.
128      * @throws IOException in case cleaning is unsuccessful
129      */

130     private static void cleanDirectoryOnExit(File JavaDoc directory)
131             throws IOException JavaDoc {
132         if(!directory.exists()) {
133             // do nothing
134
return;
135         }
136
137         if(!directory.isDirectory()) {
138             String JavaDoc message = directory + " is not a directory";
139             throw new IllegalArgumentException JavaDoc(message);
140         }
141
142         IOException JavaDoc exception = null;
143
144         File JavaDoc[] files = directory.listFiles();
145         for(int i = 0; i < files.length; i++) {
146             File JavaDoc file = files[i];
147             try {
148                 deleteOnExit(file);
149             }
150             catch(IOException JavaDoc ioe) {
151                 exception = ioe;
152             }
153         }
154
155         if(null != exception) {
156             throw exception;
157         }
158     }
159
160     /**
161      * Copy a file.
162      *
163      * @param source Source file to copy.
164      * @param dir Destination target dir.
165      * @throws java.io.IOException Failed to copy file.
166      */

167     public static File JavaDoc copy(final File JavaDoc source, final File JavaDoc dir) throws IOException JavaDoc {
168         if(source.getParentFile().equals(dir)) {
169             return source;
170         }
171         if(!dir.exists()) {
172             dir.mkdirs();
173         }
174         if(!dir.isDirectory()) {
175             throw new IOException JavaDoc("Destination must be a directory.");
176         }
177
178         File JavaDoc target = new File JavaDoc(dir, source.getName());
179         BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(source));
180         BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(target));
181         try {
182             IOs.write(in, out);
183         }
184         finally {
185             out.flush();
186             IOs.close(in);
187             IOs.close(out);
188         }
189         return target;
190     }
191
192     /**
193      * move the source file to the targe directory
194      *
195      * @param source
196      * @param dir
197      */

198     public static File JavaDoc move(final File JavaDoc source, final File JavaDoc dir) throws IOException JavaDoc {
199         File JavaDoc target = copy(source, dir);
200         source.delete();
201         return target;
202     }
203
204     /**
205      * Implements the same behaviour as the "touch" utility on Unix. It creates
206      * a new file with size 0 or, if the file exists already, it is opened and
207      * closed without modifying it, but updating the file date and time.
208      *
209      * @param file the File to touch
210      * @throws IOException If an I/O problem occurs
211      */

212     public static void touch(File JavaDoc file) throws IOException JavaDoc {
213         OutputStream JavaDoc out = new java.io.FileOutputStream JavaDoc(file);
214         IOs.close(out);
215     }
216
217     /**
218      * Convert the array of Files into a list of URLs.
219      *
220      * @param files the array of files
221      * @return the array of URLs
222      * @throws IOException if an error occurs
223      */

224     public static URL JavaDoc[] toURLs(File JavaDoc[] files) throws IOException JavaDoc {
225         URL JavaDoc[] urls = new URL JavaDoc[files.length];
226
227         for(int i = 0; i < urls.length; i++) {
228             urls[i] = files[i].toURL();
229         }
230
231         return urls;
232     }
233
234     /**
235      * Convert from a <code>URL</code> to a <code>File</code>.
236      *
237      * @param url File URL.
238      * @return The equivalent <code>File</code> object, or <code>null</code> if the URL's protocol
239      * is not <code>file</code>
240      */

241     public static File JavaDoc toFile(URL JavaDoc url) {
242         if(url.getProtocol().equals("file") == false) {
243             return null;
244         }
245         else {
246             String JavaDoc filename =
247                     url.getFile().replace('/', File.separatorChar);
248             return new File JavaDoc(filename);
249         }
250     }
251
252     /**
253      * get file content
254      */

255     public static String JavaDoc content(File JavaDoc file) throws IOException JavaDoc {
256         InputStream JavaDoc in = new java.io.FileInputStream JavaDoc(file);
257         try {
258             return IOs.toString(in);
259         }
260         finally {
261             IOs.close(in);
262         }
263     }
264
265     /**
266      * list Files recursively
267      *
268      * @param filter
269      * @return File list
270      */

271     public static List JavaDoc listFiles(File JavaDoc dir, FileFilter JavaDoc filter) {
272         List JavaDoc files = new ArrayList JavaDoc();
273         if(!dir.exists() || dir.isFile()) return files;
274         listFiles(files, dir, filter);
275         return files;
276     }
277
278     private static void listFiles(List JavaDoc filesList, File JavaDoc dir, FileFilter JavaDoc filter) {
279         File JavaDoc[] files = dir.listFiles(filter);
280         List JavaDoc temp = Arrays.asList(files);
281         Collections.sort(temp);
282         filesList.addAll(temp);
283
284         File JavaDoc[] subDirs = dir.listFiles(FileFilters.directoryFileFilter());
285         for(int i = 0; i < subDirs.length; i++) {
286             listFiles(filesList, subDirs[i], filter);
287         }
288     }
289 }
290
291
Popular Tags