1 11 12 package org.eclipse.osgi.framework.internal.defaultadaptor; 13 14 import java.io.*; 15 import java.util.Properties ; 16 17 21 public class MetaData { 22 23 26 Properties properties = new Properties (); 27 28 31 File datafile; 32 33 36 String header; 37 38 44 public MetaData(File datafile, String header) { 45 this.datafile = datafile; 46 this.header = header; 47 } 48 49 56 public String get(String key, String def) { 57 return properties.getProperty(key, def); 58 } 59 60 68 public int getInt(String key, int def) { 69 String result = get(key, null); 70 if (result == null) { 71 return def; 72 } 73 try { 74 return Integer.parseInt(result); 75 } catch (NumberFormatException nfe) { 76 return def; 77 } 78 } 79 80 88 public long getLong(String key, long def) { 89 String result = get(key, null); 90 if (result == null) { 91 return def; 92 } 93 try { 94 return Long.parseLong(result); 95 } catch (NumberFormatException nfe) { 96 return def; 97 } 98 } 99 100 107 public boolean getBoolean(String key, boolean def) { 108 String result = get(key, null); 109 if (result == null) { 110 return def; 111 } 112 return Boolean.valueOf(result).booleanValue(); 113 } 114 115 120 public void set(String key, String val) { 121 properties.put(key, val); 122 } 123 124 129 public void setInt(String key, int val) { 130 properties.put(key, Integer.toString(val)); 131 } 132 133 138 public void setLong(String key, long val) { 139 properties.put(key, Long.toString(val)); 140 } 141 142 147 public void setBoolean(String key, boolean val) { 148 properties.put(key, new Boolean (val).toString()); 149 } 150 151 155 public void remove(String key) { 156 properties.remove(key); 157 } 158 159 163 public void save() throws IOException { 164 if (!datafile.exists() && datafile.getParent() != null) { 165 File parent = new File(datafile.getParent()); 166 if (!parent.exists()) 167 parent.mkdir(); 168 } 169 170 FileOutputStream fos = new FileOutputStream(datafile); 171 try { 172 properties.store(fos, header); 173 } finally { 174 fos.close(); 175 } 176 } 177 178 182 public void load() throws IOException { 183 properties.clear(); 184 if (datafile.exists()) { 185 FileInputStream fis = new FileInputStream(datafile); 186 try { 187 properties.load(fis); 188 } finally { 189 fis.close(); 190 } 191 } 192 } 193 194 197 public String toString() { 198 return properties.toString(); 199 } 200 201 } 202 | Popular Tags |