Java獲取.properties文件

	@SuppressWarnings("rawtypes")
	public static void getProperties() {
		
		Properties properties = null;
		InputStream in = null;
		try {
			properties = new Properties();
	
			//獲取文件
			in = Object.class.getResourceAsStream("/config.properties");
			properties.load(in);
			
			Enumeration enumerations= properties.propertyNames();
			
			//遍歷輸出
			while(enumerations.hasMoreElements()){
				String key=(String)enumerations.nextElement();
				String value=properties.getProperty(key);
				
				System.out.println(key+"="+value);		
			}
			
		} catch (IOException e) {
			e.printStackTrace();

		//關閉InputStream
		} finally{
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章