KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > io > Files


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.io;
5
6 import java.io.File JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Set JavaDoc;
10
11 /**
12  * Utility methods for dealing with files.
13  *
14  * @author Rob Gordon
15  */

16 public class Files {
17
18     /**
19      * Expand an array of files which could contain wildcards into
20      * an actual array of files.
21      *
22      * @param files The in files.
23      * @return The expanded files
24      */

25     public static File JavaDoc[] expand(File JavaDoc[] files) {
26         Set JavaDoc results = new HashSet JavaDoc();
27         for (int i = 0; i < files.length; ++i) {
28             results.addAll(Arrays.asList(new WildcardSpec(files[i]).findFiles()));
29         }
30         return (File JavaDoc[]) results.toArray(new File JavaDoc[0]);
31     }
32     
33     /**
34      * Verify that all files are readable.
35      *
36      * @param files The files.
37      * @throw RuntimeExceptoin if on isn't readable.
38      */

39     public static void verifyReadable(File JavaDoc[] files)
40     throws RuntimeException JavaDoc {
41         for (int i = 0; i < files.length; ++i) {
42             if (!files[i].exists()) {
43                 throw new RuntimeException JavaDoc("File " + files[i] + " does not exist.");
44             }
45             if (!files[i].canRead()) {
46                 throw new RuntimeException JavaDoc("File " + files[i] + " can not be read.");
47             }
48         }
49     }
50
51     /**
52      * Verify that files are writeable.
53      *
54      * @param files The files.
55      * @throw RuntimeException if one of the files isn't writeable.
56      */

57     public static void verifyWrite(File JavaDoc[] files)
58     throws RuntimeException JavaDoc {
59         for (int i = 0; i < files.length; ++i) {
60             if (!files[i].exists()) {
61                 throw new RuntimeException JavaDoc("File " + files[i] + " does not exist.");
62             }
63             if (!files[i].canWrite()) {
64                 throw new RuntimeException JavaDoc("File " + files[i] + " can not be changed.");
65             }
66         }
67     }
68 }
69
Popular Tags