Java之读取Properties属性文件【整理】
2016-04-26 23:30
495 查看
项目的目录结构
使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,public class PropertiesFileReadTest {/*** 使用getResourceAsStream方法读取properties文件*/static void readPropFileByGetResourceAsStream() {/*** 读取src下面config.properties包内的配置文件* test1.properties位于config.properties包内*/InputStream in1 = PropertiesFileReadTest.class.getClassLoader().getResourceAsStream("config/properties/test1.properties");/*** 读取和PropertiesFileReadTest类位于同一个包里面的配置文件* test2.properties和PropertiesFileReadTest类在同一个包里面*/InputStream in2 = PropertiesFileReadTest.class.getResourceAsStream("test2.properties");/*** 读取src根目录下文件的配置文件* jdbc.properties位于src目录*/InputStream in3 = PropertiesFileReadTest.class.getClassLoader().getResourceAsStream("jdbc.properties");/*** 读取位于另一个source文件夹里面的配置文件* config是一个source文件夹,config.properties位于config source文件夹中*/InputStream in4 = PropertiesFileReadTest.class.getClassLoader().getResourceAsStream("config.properties");Properties p = new Properties();p.load(in1);System.out.println("test1.properties:name=" + p.getProperty("name")+ ",age=" + p.getProperty("age"));其他略....}/*** 使用InPutStream流读取properties文件*/static void readPropFileByInPutStream() {/*** 读取src根目录下文件的配置文件* jdbc.properties位于src目录*/InputStream in1 = new BufferedInputStream(new FileInputStream("src/jdbc.properties"));/*** 读取src下面config.properties包内的配置文件* test1.properties位于config.properties包内*/InputStream in2 = new BufferedInputStream(new FileInputStream("src/config/properties/test1.properties"));/*** 读取和PropertiesFileReadTest类位于同一个包里面的配置文件* test2.properties和PropertiesFileReadTest类在同一个包里面*/InputStream in3 = new BufferedInputStream(new FileInputStream("src/propertiesFile/read/test/test2.properties"));/*** 读取位于另一个source文件夹里面的配置文件* config是一个source文件夹,config.properties位于config source文件夹中*/InputStream in4 = new FileInputStream("config/config.properties");Properties p = new Properties();p.load(in1);其他略....}}以上参考自博客:http://www.cnblogs.com/xdp-gacl/p/3640211.htmlClassPath : 指的是:WEB-INF\classes这个目录下,通常把文件放在src文件夹下,或者新建文件并且添加到build path即可。IDE在同步文件到tomcat时会自动将文件放在classes下。InputStream in = this.getClass().getResourceAsStream("路径名"); //一般为“/”相对classpath的路径,这样就不需要读取绝对路径了在java web项目中添加配置文件,满足数据库配置参数以及其他自定义参数存放,可自己写一个配置文件**.properties,把项目所需的自定义配置信息以名值对的形式写入文件。把它放到项目的源文件包src下,部署时IDE会自动将其复制到相应路径。 java web项目未开启服务(有没有部署到tomcat中一样)使用this.getClass().getResource("/")得到Url对象,打印如下: file:/F:/zgh/myworkspace/collection/WebRoot/WEB-INF/classes/ 此路径为java web项目源路径。 部署到tomcat中的java web项目开启服务后使用this.getClass().getResource("/")得到Url对象,打印如下: file:/F:/zgh/software/apache-tomcat-6.0.14/webapps/collection/WEB-INF/classes/ java application项目使用this.getClass().getResource("/")得到Url对象,打印如下: file:/F:/zgh/myworkspace/javaeye/bin/ 此路径为java application项目的class文件的根路径。 注意:未开启tomcat web服务时打印的不是上面的路径,是你的项目源文件的classes路径。
相关文章推荐
- java学习写的第一个东西《飞机大战》
- spring框架学习笔记(八)
- 【java web】--Beetl+html鼠标悬停提示气泡
- Spring @Service生成bean名称的规则
- IMF传奇行动第83课:Spark Streaming第二课:用Scala和Java二种方式实战Spark Streaming开发
- java.lang.ClassNotFoundException: net.sf.cglib.proxy.Callback
- java之代理模式
- java爬虫学习日记1-基本爬虫原理介绍
- java爬虫学习日记2-宽度优先爬虫代码实现
- java 抽象类 抽象方法 abstract
- Java Web基础知识之Filter:过滤一切你不想看到的事情
- javase tutorial
- 包、修饰符、内部类、匿名内部类(java基础知识十)
- eclipse启动Android调试出错:The connection to adb is down, and a severe error has occured.
- 如何在ubuntu中创建eclipse的快捷方式
- Java程序猿学习当中各个阶段的建议
- java class类
- java的垃圾回收器
- springMVC(6)------springMVC使用POJO对象绑定请求参数值
- 20145305 《Java程序设计》实验四