KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > util > PropertiesFile


1 package fr.jayasoft.ivy.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.FileOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Properties JavaDoc;
8
9
10 /**
11  * A simple Properties extension easing the loading and saving of data
12  */

13 public class PropertiesFile extends Properties JavaDoc {
14     private File JavaDoc _file;
15     private String JavaDoc _header;
16
17     public PropertiesFile(File JavaDoc file, String JavaDoc header) {
18         _file = file;
19         _header = header;
20         if (_file.exists()) {
21             FileInputStream JavaDoc fis = null;
22             try {
23                 fis = new FileInputStream JavaDoc(_file);
24                 load(fis);
25             } catch (Exception JavaDoc ex) {
26                 Message.warn("exception occured while reading properties file "+_file+": "+ex.getMessage());
27             }
28             try {
29                 if (fis != null) {
30                     fis.close();
31                 }
32             } catch (IOException JavaDoc e) {
33             }
34         }
35     }
36
37     public void save() {
38         FileOutputStream JavaDoc fos = null;
39         try {
40             if (_file.getParentFile() != null && !_file.getParentFile().exists()) {
41                 _file.getParentFile().mkdirs();
42             }
43             fos = new FileOutputStream JavaDoc(_file);
44             store(fos, _header);
45         } catch (Exception JavaDoc ex) {
46             Message.warn("exception occured while writing properties file "+_file+": "+ex.getMessage());
47         }
48         try {
49             if (fos != null) {
50                 fos.close();
51             }
52         } catch (IOException JavaDoc e) {
53         }
54     }
55
56 }
Popular Tags