properties属性文件

来源:互联网 发布:stm8单片机自学笔记pdf 编辑:程序博客网 时间:2024/06/05 10:12
properties属性文件内容都是以键值对形式存在的,比如我写一个叫test.properties的文件,打开后可以再里面写如:name=Tom
而在java类中需要new一个Properties类的对象,如下:
Properties properties = new Properties();
接下来需要获取test.properties的文件路径:
String path = Thread.currentThread().getContextClassLoader().getResource("test.properties").getPath();
然后加载该文件:
properties.load(new FileInputStream(path));
最后你就可以get它的属性了:
String name_1=properties.getProperty("name");
这个name_1的值就是“TOM”了。
(因为涉及到文件流,所以加载那一步需要try catch,根据编译器提示自己加吧)




经常看到java程序中带properties文件(包括log4j,junit的使用),作为一个java所知甚少的程序员,有必要搞清楚这个问题.


首先,这个文件经常是作为资源文件的,maven,sbt中资源文件一般还独立一个src/java/resource文件夹,资源文件一般是通过getResouce,getResourceAsStream(获取对应InputStream)获得.如:


Test.class.getResource("file3.txt")


Test.class.getResource("/file3.txt")


Test.class.getClassLoader.getResource("/file3.txt")


参数可以是相对路径和绝对路径,相对路径是在对应class文件(classpath)下找的,绝对路径则是顶层classloader那一层找的.


properties文件使用需要import java.util.Properties,如在scala shell中输入:


import java.util.Properties
val x = getClass.getResourceAsStream("/a.properties")
val prop = new Properties
prop.load(x)
prop.getProperty("a") ==> 返回null
prop.getProperty("IcisReport.contextPath") ==> 返回/IcisReport
对应的a.properties如:


#   IcisReport的ip
IcisReport.server.ip=192.168.3.143
#   IcisReport的端口
IcisReport.server.port=8080
#   IcisReport的上下文路径
IcisReport.contextPath=/IcisReport


而java中System.getProperty可以在java命令中使用-D定义,log4j中读取配置文件是这样的:


PropertyConfigurator.configure("classpath:conf/log4j.properties");
或:
PropertyConfigurator.configure(this.getClass().getClassLoader().getResource("conf/log4j.properties)
可以通过System.setProperty改变相关属性.


参考:


java resource路径总结:http://enetor.iteye.com/blog/1125189


http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html
http://stackoverflow.com/questions/10730415/loading-log4j-properties-from-a-package-in-java










java解析properties文件的几种方法及用法


1、使用java.util.Properties类的load()方法
示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);


 


2、使用java.util.ResourceBundle类的getBundle()方法
示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());


 


3、使用java.util.PropertyResourceBundle类的构造函数
示例: InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);


 


4、使用class变量的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);


 


5、使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);


 


6、使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例: InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);


 


补充
Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
0 0
原创粉丝点击