import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.MissingResourceException;import java.util.Properties;import java.util.regex.Pattern;
public final class CommonProperties {
//获取资源文件 public static final String projectfile = "g:/opt/udbs/config/resources.properties"; private static CommonProperties instance = null; private Properties propertie; private FileInputStream inputFile;
private CommonProperties() {
propertie = new Properties(); try { inputFile = new FileInputStream(projectfile); propertie.load(inputFile); inputFile.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } }
public static CommonProperties instance() { if (instance == null) { instance = new CommonProperties(); } return instance; }
public String getString(String skey) { String result = ""; try { result = propertie.getProperty(skey); if (result != null) { result = new String(result.getBytes("ISO8859-1"), System .getProperty("file.encoding")); } } catch (NullPointerException e) { return ""; } catch (MissingResourceException e) { return ""; } catch (ClassCastException e) { return ""; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }
public int getInt(String skey) { String value = getString(skey); if (Pattern.matches("}", value)) { return Integer.parseInt(value); } else { return 0; }
}
}