如何使用Properties配置文件

工具:eclipse(Myeclipse)
知識面:javase IO流/文件配置

1.步驟:創建Properties對象
2.創建文件路徑
3.加載資源配置
4.關閉文件路徑

5.使用加載資源對象properties調用getProperties()

配置文件的使用:#這個是驅動名
driver=oracle.jdbc.driver.OracleDriver
#這個是數據庫路徑
url=jdbc:oracle:thin:@<域名>.<端口>.<SID>
#這個是數據庫用戶名
username=xxx
#這個是數據庫密碼
password=xxx

//配置對象
		try {
		Properties properties = new Properties();
		//配置文件路徑
		InputStream inStream 
			= PropertiesDemo.class.
				getClassLoader().
				getResourceAsStream(
				"com/csdn/jdbcdemo/date2017_11_12/propertis.txt");
		//加載資源配置	
		properties.load(inStream);
		
		//關閉配置文件路徑
		inStream.close();
		
		//獲取配置文件中的信息
		
		//獲取驅動名
		String driver = properties.getProperty("driver");
		//獲取路徑
		String url = properties.getProperty("url");
		//獲取用戶名
		String username = properties.getProperty("username");
		//獲取密碼
		String password = properties.getProperty("password");
		
		System.out.println(driver+"\n"+url+"\n"+username+"\n"+password);
		
		
		} catch (IOException e) {
			e.printStackTrace();
		}


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