Java读取Properties配置文件

	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();
		} 
	}

猜你喜欢

转载自neuq4090102201507072652.iteye.com/blog/2254637