KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBPropertyFileReader


1 package com.ca.commons.cbutil;
2
3 import java.io.*;
4 import java.util.Properties JavaDoc;
5
6 /**
7  * Reads a property file from a given location.
8  *
9  * @author erstr01
10  */

11 public class CBPropertyFileReader
12 {
13     /**
14      * The path and file name of the file to read.
15      */

16     private String JavaDoc propFileName = null;
17
18     /**
19      * The object that the file is loaded into.
20      */

21     private Properties JavaDoc properties = null;
22
23     /**
24      * Reads a property file.
25      *
26      * @param propFileName the path and name of the file to read.
27      */

28     public CBPropertyFileReader(String JavaDoc propFileName)
29             throws IOException
30     {
31         this.propFileName = propFileName;
32         File propFile = new File(propFileName);
33         loadPropertyFile(propFile);
34     }
35
36     /**
37      * Reads a property file.
38      *
39      * @param propFile the property file.
40      */

41     public CBPropertyFileReader(File propFile)
42             throws IOException
43     {
44         if (propFile == null || !propFile.exists())
45             throw new IOException("File does not exist.");
46
47         propFileName = propFile.getAbsolutePath();
48         loadPropertyFile(propFile);
49     }
50
51     /**
52      * Sets up the reader.
53      *
54      * @param propFile the property file.
55      */

56     private void loadPropertyFile(File propFile)
57             throws IOException
58     {
59         // Sanity Check that file exists...
60
if (propFile.exists() == false)
61             throw new IOException("Cannot find the properties file: " + propFileName);
62
63         properties = new Properties JavaDoc();
64         properties.load(new FileInputStream(propFile));
65     }
66
67     /**
68      * Wraps Property.getProperty().
69      *
70      * @param key the key in the property file.
71      * @return the value pertaining to the supplied key.
72      */

73     public String JavaDoc getValue(String JavaDoc key)
74     {
75         return properties.getProperty(key);
76     }
77
78     /**
79      * @return the property file name.
80      */

81     public String JavaDoc getPropFileName()
82     {
83         return propFileName;
84     }
85
86     /**
87      * Sets the property file name.
88      *
89      * @param propFileName the property file name including it's path.
90      */

91     public void setPropFileName(String JavaDoc propFileName)
92     {
93         this.propFileName = propFileName;
94     }
95
96     /**
97      * @return the object that the property file has been read in to.
98      */

99     public Properties JavaDoc getProperties()
100     {
101         return properties;
102     }
103
104     /**
105      * Set the property object.
106      *
107      * @param properties the object that the property file has been read into.
108      */

109     public void setProperties(Properties JavaDoc properties)
110     {
111         this.properties = properties;
112     }
113 }
114
Popular Tags