KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > PropertiesUtil


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.util;
4
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 /**
13  * Misc java.util.Properties utils.
14  */

15 public class PropertiesUtil {
16
17     // ---------------------------------------------------------------- to/from files
18

19     /**
20      * Create properties from the file.
21      *
22      * @param fileName properties file name to load
23      *
24      * @exception IOException
25      */

26     public static Properties JavaDoc createFromFile(String JavaDoc fileName) throws IOException JavaDoc {
27         return createFromFile(new File JavaDoc(fileName));
28     }
29
30     /**
31      * Create properties from the file.
32      *
33      * @param file properties file to load
34      *
35      * @exception IOException
36      */

37     public static Properties JavaDoc createFromFile(File JavaDoc file) throws IOException JavaDoc {
38         Properties JavaDoc prop = new Properties JavaDoc();
39         loadFromFile(prop, file);
40         return prop;
41     }
42
43     /**
44      * Loads properties from the file. Properties are appended to the existing
45      * properties object.
46      *
47      * @param p properties to fill in
48      * @param fileName properties file name to load
49      *
50      * @exception IOException
51      */

52     public static void loadFromFile(Properties JavaDoc p, String JavaDoc fileName) throws IOException JavaDoc {
53         loadFromFile(p, new File JavaDoc(fileName));
54     }
55
56     /**
57      * Loads properties from the file. Properties are appended to the existing
58      * properties object.
59      *
60      * @param p properties to fill in
61      * @param file file to read properties from
62      *
63      * @exception IOException
64      */

65     public static void loadFromFile(Properties JavaDoc p, File JavaDoc file) throws IOException JavaDoc {
66         FileInputStream JavaDoc fis = null;
67         try {
68             fis = new FileInputStream JavaDoc(file);
69             p.load(fis);
70         } finally {
71             if (fis != null) {
72                 fis.close();
73             }
74         }
75     }
76
77
78     /**
79      * Writes properties to a file.
80      *
81      * @param p properties to write to file
82      * @param fileName destination file name
83      *
84      * @exception IOException
85      */

86     public static void writeToFile(Properties JavaDoc p, String JavaDoc fileName) throws IOException JavaDoc {
87         writeToFile(p, new File JavaDoc(fileName), null);
88     }
89
90     /**
91      * Writes properties to a file.
92      *
93      * @param p properties to write to file
94      * @param fileName destiantion file name
95      * @param header optional header
96      *
97      * @exception IOException
98      */

99     public static void writeToFile(Properties JavaDoc p, String JavaDoc fileName, String JavaDoc header) throws IOException JavaDoc {
100         writeToFile(p, new File JavaDoc(fileName), header);
101     }
102
103     /**
104      * Writes properties to a file.
105      *
106      * @param p properties to write to file
107      * @param file destination file
108      *
109      * @exception IOException
110      */

111     public static void writeToFile(Properties JavaDoc p, File JavaDoc file) throws IOException JavaDoc {
112         writeToFile(p, file, null);
113     }
114
115     /**
116      * Writes properties to a file.
117      *
118      * @param p properties to write to file
119      * @param file destiantion file
120      * @param header optional header
121      *
122      * @exception IOException
123      */

124     public static void writeToFile(Properties JavaDoc p, File JavaDoc file, String JavaDoc header) throws IOException JavaDoc {
125         FileOutputStream JavaDoc fos = null;
126         try {
127             fos = new FileOutputStream JavaDoc(file);
128             p.store(fos, header);
129         } finally {
130             if (fos != null) {
131                 fos.close();
132             }
133         }
134     }
135
136
137     // ---------------------------------------------------------------- subsets
138

139     /**
140      * Creates new Properties object from the original one, by copying
141      * those properties that have specified first part of the key name.
142      * Prefix may be optionally stripped during this process.
143      *
144      * @param p source properties, from which new object will be created
145      * @param prefix key names prefix
146      *
147      * @return subset properties
148      */

149     public static Properties JavaDoc subset(Properties JavaDoc p, String JavaDoc prefix, boolean stripPrefix) {
150         if (StringUtil.isBlank(prefix) == true) {
151             return p;
152         }
153         if (prefix.endsWith(".") == false) {
154             prefix += '.';
155         }
156         Properties JavaDoc result = new Properties JavaDoc();
157         int baseLen = prefix.length();
158         Iterator JavaDoc iti = p.keySet().iterator();
159         while (iti.hasNext()) {
160             String JavaDoc key = (String JavaDoc) iti.next();
161             if (key.startsWith(prefix) == true) {
162                 result.setProperty(stripPrefix == true ? key.substring(baseLen) : key, p.getProperty(key));
163             }
164         }
165         return result;
166     }
167
168 }
169
Popular Tags