读取properties文件

     假设项目名称为myproject

 

public class UtilConfig {
	
	private static final Properties prop;
	
	static {
		prop = new Properties();
		try {
			String PROPERTIES_FILE_SYSTEM_VAR = "moral.website.properties";
			String propertiesFile = System.getProperty(PROPERTIES_FILE_SYSTEM_VAR);
			if ( propertiesFile == null ) {
				propertiesFile = "/setup/myproject.properties";
			}
			prop.load(UtilConfig.class.getResourceAsStream(propertiesFile));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static int getInt(String key) {
		return getInt(key, 100);
	}
	
	public static int getInt(String key, int defaultValue) {
		try {
			return  Integer.parseInt(prop.getProperty(key, String.valueOf(defaultValue)));
		} catch (Exception e) {
			return defaultValue;
		}
	}
	
	public static String getString(String key, String defaultValue) {
		return prop.getProperty(key, defaultValue);
	}
	
	public static String getString(String key) {
		return prop.getProperty(key);
	}
	
	public static long getLong(String key) {
		return getLong(key, 1000L);
	}
	
	public static long getLong(String key, long defaultValue) {
		try {
			return  Long.parseLong(prop.getProperty(key, String.valueOf(defaultValue)));
		} catch (Exception e) {
			return defaultValue;
		}
	}
}


 




 

posted @ 2015-08-01 09:53  wala-wo  阅读(182)  评论(0编辑  收藏  举报