01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package org.apache.ivy.util;
19:
20: import java.io.File;
21: import java.io.FileInputStream;
22: import java.io.FileOutputStream;
23: import java.io.IOException;
24: import java.util.Properties;
25:
26: /**
27: * A simple Properties extension easing the loading and saving of data
28: */
29: public class PropertiesFile extends Properties {
30: private File file;
31:
32: private String header;
33:
34: public PropertiesFile(File file, String header) {
35: this .file = file;
36: this .header = header;
37: if (file.exists()) {
38: FileInputStream fis = null;
39: try {
40: fis = new FileInputStream(file);
41: load(fis);
42: } catch (Exception ex) {
43: Message
44: .warn("exception occured while reading properties file "
45: + file + ": " + ex.getMessage());
46: }
47: try {
48: if (fis != null) {
49: fis.close();
50: }
51: } catch (IOException e) {
52: // ignored
53: }
54: }
55: }
56:
57: public void save() {
58: FileOutputStream fos = null;
59: try {
60: if (file.getParentFile() != null
61: && !file.getParentFile().exists()) {
62: file.getParentFile().mkdirs();
63: }
64: fos = new FileOutputStream(file);
65: store(fos, header);
66: } catch (Exception ex) {
67: Message
68: .warn("exception occured while writing properties file "
69: + file + ": " + ex.getMessage());
70: }
71: try {
72: if (fos != null) {
73: fos.close();
74: }
75: } catch (IOException e) {
76: // ignored
77: }
78: }
79:
80: }
|