Properties读取properties文件

  在编写一个项目时,我们会时常修改一些配置变量来适应不同的运行环境,同时也让用户能够脱离程序去修改相关的变量设置。通常我们将这些变量定义在后缀名为properties的文件中,文件内容的格式为“键=值”,文本用“#”注释。
  而java提供了java.util.Properties来读取这些文件,具体Properties参见凭海临风的博客:http://www.cnblogs.com/bakari/p/3562244.html
  它提供了几个主要的方法:
  1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
  2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
  3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
  4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
  5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
  要获取定义在.properties中的值,首先要用Properties的方法load(InputStream inStream)来装载配置文件从而获取配置文件中的键值对,形成属性列表,其次用getProperty(String key)来获取属性列表中对应的值。
  此次主要有两种读取.properties文件的方法(在Java Project中):
  jdbc.properties:
driverClass = com.mysql.jdbc.Driver
jdbcURL=jdbc:mysql://127.0.0.1:3306/protest?useUnicode=true&characterEncoding=utf-8 
user = root
password = root

  1.使用FileInputStream("资源名称绝对路径")
    
public Properties propertiesFromFile() throws FileNotFoundException{
		properties = new Properties();
		InputStream in = null;
		try{
			in = new BufferedInputStream(new FileInputStream(propertiesPath));
			properties.load(in);
		}catch (FileNotFoundException e){
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return properties;
	}


  2.使用getClass.getResourceAsStream("资源名称") -推荐
	public Properties propertiesFromClass(){
		properties = new Properties();
		InputStream in1 = this.getClass().getResourceAsStream(propertiesPath);
		InputStream is=PropertiesLoad.class.getResourceAsStream("/jdbc.properties");
		try {
			properties.load(in1);
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				in1.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return properties;
	}

     最后使用getProperty(key)获取properties中的值:
	PropertiesLoad proLoad = new PropertiesLoad(propertiesPath);
	String DriverClass = proLoad.propertiesFromClass().getProperty("driverClass");

对于在程序中对properties文件的修改
PropertiesLoad proLoad = new PropertiesLoad(propertiesPath);
Properties pro = proLoad.propertiesFromClass();
OutputStream out = new FileOutputStream(DBconnect.class.getResource("/jdbc1.properties").toString().substring(6));
pro.setProperty("password", password+"1");
try {
	 pro.store(out, "ceshiwenjian01");
     } catch (IOException e) {
	e.printStackTrace();
     }
结果

jdbc1.properties:

#ceshiwenjian01
#Tue Aug 30 16:06:07 GMT+08:00 2016
driverClass=com.mysql.jdbc.Driver
user=root
password=root1
jdbcURL=jdbc\:mysql\://127.0.0.1\:3306/protest?useUnicode\=true&characterEncoding\=utf-8 

在上述的例子中,如果将Properties pro = proLoad.propertiesFromClass();换成Properties pro = new Properties();那么最后jdbc1.properties中的键值对只剩下password。所以所谓的setProperties(key,value)是将键值对写放入pro中,然后pro通过store将所有的pro里的键值对写入到文件中。上例中最后呈现的结果类似于改变键值对的值是因为pro装载了原来的键值对。

注:
在我们知道如何读写一个属性文件之后,我们仍然还有很多需要注意的问题,因为load和store方法都是按照ISO-8859-1的编码方式读写属性流文件的,而ILatin1 的字符和某些特殊字符,而对于非Latin1 的字符和某些特殊字符,则要使用与字符和字符串字面值所用的类似转义序列,以值和元素的形式来表示它们。所以当我们在处理中文时,不可以在直接修改属性文件时,将中文的值赋予给属性,而是要在JAVA程序中通过setProperty方法给属性赋予中文的值,因为这样store会将中文转换成 unicode码,在读取时系统会将读取到的unicode码按系统的编码打印出来,对于中文系统,通常是GBK码,这样中文才能够正常显示。






  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring框架提供了一种便捷的方式来读取properties文件中的配置信息。在Spring中,我们可以使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer来加载properties文件。 PropertyPlaceholderConfigurer是在Spring 3.1之前版本中使用的类,用于从classpath或指定位置加载properties文件。它可以将properties文件中的键值对替换为Spring配置文件中的占位符。通过将PropertyPlaceholderConfigurer配置为Bean的一部分,我们可以在XML配置文件中使用占位符来引用properties文件中的值。例如,我们可以在XML中配置一个DataSource Bean,并通过占位符引用properties文件中的数据库连接信息。 PropertySourcesPlaceholderConfigurer是从Spring 3.1版本引入的新类,它可以更方便地处理properties文件。与PropertyPlaceholderConfigurer不同,PropertySourcesPlaceholderConfigurer允许我们使用@Value注解来直接注入properties文件中的属性。我们可以在Java配置类或XML配置文件中配置PropertySourcesPlaceholderConfigurer,并在需要的地方使用@Value注解来获取properties文件中的属性值。 无论我们使用哪种方式来读取properties文件,我们都需要确保将其配置为Spring容器的一部分,并以适当的方式加载它。通常,我们会在Spring配置文件中添加一个bean来加载properties文件,并将其配置为其他bean的属性或参数的值。 总结起来,Spring提供了两种主要的方法来读取properties文件:使用PropertyPlaceholderConfigurer或PropertySourcesPlaceholderConfigurer。这些类可以让我们轻松地将properties文件中的配置值引入到Spring容器中,并在我们的应用程序中使用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值