KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cpmake > PropertiesFile


1 /*
2  * Copyright (c) 2004, Brian Hawkins
3  * brianhks@activeclickweb.com
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20  
21
22
23 package cpmake;
24  
25 import java.util.*;
26 import java.io.*;
27
28 class PropertiesFile extends Properties
29     {
30     String JavaDoc m_FileName;
31     boolean m_nonFile;
32
33     public PropertiesFile(String JavaDoc fileName)
34         {
35         m_nonFile = false;
36         FileInputStream fReader;
37         m_FileName = fileName;
38
39         try
40             {
41             fReader = new FileInputStream(fileName);
42             if (fReader != null)
43                 {
44                 this.load(fReader);
45                 fReader.close();
46                 }
47             }
48         catch(Exception JavaDoc e)
49             {
50             m_nonFile = true;
51             //e.printStackTrace();
52
}
53             
54         if (m_nonFile)
55             {
56             try
57                 {
58                 InputStream in = this.getClass().getResourceAsStream(fileName);
59                 if (in != null)
60                     {
61                     this.load(in);
62                     in.close();
63                     }
64                 }
65             catch (IOException ioe)
66                 {
67                 System.out.println("Resource "+fileName+" failed");
68                 ioe.printStackTrace();
69                 }
70             }
71         }
72
73 //-------------------------------------------------------------------
74
public void close()
75             throws IOException
76         {
77         FileOutputStream fWriter;
78
79         try
80             {
81             fWriter = new FileOutputStream(m_FileName);
82             this.store(fWriter, "properties file");
83             fWriter.close();
84             }
85         catch(Exception JavaDoc e)
86             {
87             throw new IOException();
88             }
89
90         }
91     }
92
93
Popular Tags