java读取properties文件总结
一、java读取properties文件总结
在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下:

/* 范例名称:java读取properties文件总结
* 源文件名称:PropertiesFileReadTest.java
* 要 点:
* 1. 使用getResourceAsStream方法读取properties文件
* 2. 使用InPutStream流读取properties文件
* 3. 读取properties文件的路径写法问题
*
**/
package propertiesFile.read.test; import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties; public class PropertiesFileReadTest {
public static void main(String[] args) throws FileNotFoundException {
readPropFileByGetResourceAsAtream();
System.out.println("--------------");
//readPropFileByInPutStream();
} /*
* 使用getResourceAsAtream方法读取
*/
private static void readPropFileByGetResourceAsAtream() {
/*
* 读取src下面config.properties包内的配置文件 test1.properties位于config.properties包内
*/
InputStream inl = 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 properties = new Properties();
System.out.println("----使用getResourceAsStream方法读取properties文件----"); // 从输入字节流读取属性列表(键,值)
try {
System.out.println("-----------------------");
properties.load(inl);
System.out.println("test1.properties:name=" + properties.getProperty("name") + ",age="
+ properties.getProperty("age"));
System.out.println("-----------------------");
System.out.println("-----------------------");
properties.load(in2);
System.out.println("test2.properties:name=" + properties.getProperty("name") + ",age="
+ properties.getProperty("age"));
System.out.println("-----------------------");
properties.load(in3);
System.out.println("jdbc.properties:");
// 使用指定的格式字符串和参数返回格式化的字符串, 这里的%s是java String占位符
System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url")));
System.out.println(String.format("jdbc.usename=%s", properties.getProperty("jdbc.usename")));
System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password")));
properties.load(in4);
System.out.println("config.properties:");
// 使用给定的模式创建一个MessageFormat,并使用它来格式化给定的参数,{0}是一个java的字符串占位符
System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser")));
System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));
System.out.println(MessageFormat.format("database={0}", properties.getProperty("database")));
System.out.println("----------------------------------------------"); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(inl != null) {
try {
inl.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in2 != null) {
try {
inl.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in3 != null) {
try {
inl.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in4 != null) {
try {
inl.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}
/*
* 使用InputStream流读取properties
*/
private static void readPropFileByInPutStream() throws FileNotFoundException {
InputStream in1=null;
InputStream in2=null;
InputStream in3=null;
InputStream in4=null;
System.out.println("----使用InputStream流读取properties文件----");
try {
/*
* 读取src下面config.properties包内的配置文件 test1.properties位于config.properties包内
*/ in1 =new BufferedInputStream(new FileInputStream("src/config/properties/test1.properties"));
/*
* 读取和PropertiesFileReadTest类位于同一个包里面的配置文件
* test2.properties和PropertiesFileReadTest类在同一个包里面
*/
in2=new BufferedInputStream(new FileInputStream("src/propertiesFile/read/test/test2.properties"));
/*
* 读取src根目录下文件的配置文件
* jdbc.properties位于src目录
*/
in3 = new BufferedInputStream(new FileInputStream("src/jdbc.properties"));
/*
* 读取位于另一个source文件夹里面的配置文件
* config是一个source文件夹,config.properties位于config source文件夹中
*/
in4 = new FileInputStream("config/config.properties"); Properties properties=new Properties(); System.out.println("-----------------------");
properties.load(in1);
System.out.println("test1.properties:name=" + properties.getProperty("name") + ",age="
+ properties.getProperty("age"));
System.out.println("-----------------------");
System.out.println("-----------------------");
properties.load(in2);
System.out.println("test2.properties:name=" + properties.getProperty("name") + ",age="
+ properties.getProperty("age"));
System.out.println("-----------------------");
properties.load(in3);
System.out.println("jdbc.properties:");
// 使用指定的格式字符串和参数返回格式化的字符串, 这里的%s是java String占位符
System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url")));
System.out.println(String.format("jdbc.usename=%s", properties.getProperty("jdbc.usename")));
System.out.println(String.format("jdbc.password=%s", properties.getProperty("jdbc.password")));
properties.load(in4);
System.out.println("config.properties:");
// 使用给定的模式创建一个MessageFormat,并使用它来格式化给定的参数,{0}是一个java的字符串占位符
System.out.println(MessageFormat.format("dbuser={0}", properties.getProperty("dbuser")));
System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));
System.out.println(MessageFormat.format("database={0}", properties.getProperty("database")));
System.out.println("----------------------------------------------"); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (in1 != null) {
try {
in1.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (in2 != null) {
try {
in2.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (in3 != null) {
try {
in3.close();
} catch (IOException e) {
e.printStackTrace();
}
} if (in4 != null) {
try {
in4.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
java读取properties文件总结的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java读取properties文件时候要注意的地方
java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
- java基础—java读取properties文件
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- Java基础学习总结(15)——java读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java读取.properties文件
在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...
- Java 读取Properties文件时应注意的路径问题
1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题: InputStream in = this.getClass().getRe ...
随机推荐
- Scrapy下载图片及自定义分类下载路径
配置下载图片的流程如下 在items中定义两个属性,image_urls 和images .image_urls是用来存储需要下载的图片url链接,列表类型: 当文件下载完成后会把相关下载信息存入im ...
- 责任链模式(Chain of Responsibility、Handler)(请求处理建立链)
(使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止.) 从名字中看出 ,系统中将会存在多个有类似处理能力的对 ...
- dl, dt, dd /line-height /loat /vertical-align 问题
刚刚在看张鑫旭大神的个人网站,看到一篇关于“css瓶颈”的深度好文,地址为:http://www.zhangxinxu.com/wordpress/?p=2523 关于张大神在文章里面提到的四个问题: ...
- spring源码学习之bean的加载(三)
接着二中的继续写,那个都超过1000行了,哈,需要重新写一个,要不太长了,我都看不下去了 7.4 初始化bean doCreateBean函数中有这样一行代码:这行代码中initializeBean函 ...
- 如约而至(walk)
LCA大佬的做法: 考虑暴力的高斯消元,我们优化它. $\sum\limits_{j} gcd(i,j)^{c-d} i^d j^d x_j=b_i$ $\sum\limits_{j} gcd(i,j ...
- BaiduTemplate [ 百度模板引擎 ]
地址: http://baidufe.github.io/BaiduTemplate/
- python 升级后正确安装 pip
由于服务器的python 版本是2.6.6 , 为了使用 twisted 升级至 2.7.13 , 如果此时直接用 yum install python-pip 安装 pip, 则实际pip 会安装在 ...
- SPSS正交设计的操作
SPSS正交设计的操作 设要做二因素的正交设计,A因素有三个水平,B因素有两个水平.则选择Data-->Orthogonal Design-->generate,弹出的就是正交设计窗口: ...
- Codeforces Round #573 (Div. 2)
A:Tokitsukaze and Enhancement 当时看错条件了..以为A>C>B>D.就胡写了判断条件. #include<bits/stdc++.h> us ...
- C#基础之Async和Await 的异步编程
官方文档地址:https://docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/concepts/async/ Coffee cup = ...