`
E路之吕
  • 浏览: 15062 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java读取Properties配置文件

    博客分类:
  • Java
阅读更多
	private String driver;
	private String url;
	private String user;
	private String password;
	private Properties props = new Properties();

	private void loadResorces() {
		try {
			/**
			 * 读取WEB-INF文件夹下的Properties文件
			 */
			String filePath = this.getClass().getResource("/").getPath();
			filePath = filePath.substring(1, filePath.indexOf("classes"));
			FileInputStream inputFile = new FileInputStream(filePath + "jdbc.properties");
			
			/**
			 * 读取默认文件夹下的Properties文件
			 * InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
			 */
			props.load(inputFile);
			url = props.getProperty("jdbc.url");
			driver = props.getProperty("jdbc.driver");
			user = props.getProperty("jdbc.user");
			password = props.getProperty("jdbc.password"); 
			inputFile.close(); 
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics