| 1 3 package edu.neu.ccs.jmk.swing; 4 5 22 23 import java.util.*; 24 import java.io.*; 25 26 public final 27 class JmkProperties 28 { 29 30 private final static String JMK_PROPERTIES = "jmk.properties"; 31 32 private Properties properties_ = null ; 34 private static JmkProperties instance_ = null; 35 36 private JmkProperties() 37 { 38 properties_=new Properties() ; 39 } 40 public static JmkProperties getInstance() 41 { 42 if (instance_==null) 43 instance_ = new JmkProperties(); 44 return(instance_); 45 } 46 47 49 public String getString(String _key, String _defaultValue) 50 { 51 String value = properties_.getProperty(_key); 52 if (value == null) 53 { 54 return(_defaultValue); 55 } 56 else 57 { 58 value=value.trim(); 59 return(value); 60 } 61 } 62 63 public boolean getBoolean(String _key, boolean _defaultValue) 64 { 65 String value = properties_.getProperty(_key); 66 if (value == null) 67 { 68 return(_defaultValue); 69 } 70 else 71 { 72 value=value.trim(); 73 if (value.toUpperCase().compareTo("TRUE")==0) 74 return(true); 75 else if (value.toUpperCase().compareTo("FALSE")==0) 76 return(false); 77 else 78 { 79 System.err.println("Invalid boolean property for key <"+_key+">"); 80 return (_defaultValue); 81 } 82 } 83 } 84 85 public int getInteger(String _key, int _defaultValue) 86 { 87 88 String value = properties_.getProperty(_key); 89 if (value == null) 90 { 91 return(_defaultValue); 92 } 93 else 94 { 95 value=value.trim(); 96 try 97 { 98 int v=Integer.parseInt(value); 99 return(v); 100 } 101 catch (NumberFormatException e) 102 { 103 System.err.println("Type mismatch for key <"+_key+">, defaulting to <"+_defaultValue+">"); 104 return(_defaultValue); 105 } 106 } 107 } 108 109 public String put(String _key, String _value) 110 { 111 return(String )properties_.put(_key, _value); 112 } 113 114 public void load() 116 { 117 try 118 { 119 InputStream fis = new FileInputStream(JMK_PROPERTIES); 120 properties_.load(fis); 121 fis.close(); 122 } 123 catch (Exception e) 124 { 125 } 127 } 128 129 public void save() 131 { 132 try 133 { 134 FileOutputStream fos = new FileOutputStream(JMK_PROPERTIES); 135 properties_.store(fos, "Jmk property file"); 136 fos.close(); 137 properties_.clear(); 138 } 139 catch (IOException e) 140 { 141 System.err.println(e); 142 } 143 } 144 145 } 146 | Popular Tags |