| 1 30 31 package rcm.util; 32 33 import java.util.*; 34 import java.io.*; 35 36 public class Config extends Properties { 37 File file; 38 IOException lastException; 39 40 public Config (String fileBaseName) { 41 this (fileBaseName, null); 42 } 43 44 public Config (File file) { 45 this (file, null); 46 } 47 48 public Config (String fileBaseName, Config defaults) { 49 this (new File (getHomeDirectory (), fileBaseName), 50 defaults); 51 } 52 53 public Config (File file, Config defaults) { 54 super (defaults); 55 56 this.file = file; 57 try { 58 FileInputStream in = 59 new FileInputStream (file); 60 load (in); 61 in.close (); 62 } catch (IOException e) { 63 lastException = e; 64 } 65 } 66 67 public IOException getLastException () { 68 return lastException; 69 } 70 71 78 public void save () { 79 try { 80 FileOutputStream out = new FileOutputStream (file); 81 save (out, ""); 82 out.close (); 83 lastException = null; 84 } catch (IOException e) { 85 lastException = e; 86 } 87 } 88 89 public int countKeysStartingWith (String prefix) { 90 int n = 0; 91 for (Enumeration e = propertyNames (); e.hasMoreElements (); ) { 92 String name = (String )e.nextElement (); 93 if (name.startsWith (prefix)) 94 ++n; 95 } 96 return n; 97 } 98 99 public void removeAllKeysStartingWith (String prefix) { 100 Vector keysToDelete = new Vector (); 101 for (Enumeration e = propertyNames (); e.hasMoreElements (); ) { 102 String name = (String )e.nextElement (); 103 if (name.startsWith (prefix)) 104 keysToDelete.addElement (name); 105 } 106 107 for (Enumeration e = keysToDelete.elements (); 108 e.hasMoreElements (); ) 109 remove (e.nextElement ()); 110 } 111 112 public static File getHomeDirectory () { 113 String homedir; 114 if ((homedir = System.getProperty ("user.home")) == null 115 && (homedir = System.getProperty ("user.dir")) == null) 116 homedir = "."; 117 118 return new File (homedir); 119 } 120 } 121 | Popular Tags |