一、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文件总结的更多相关文章

  1. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  2. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  3. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  4. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  5. java读取properties文件时候要注意的地方

    java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...

  6. java基础—java读取properties文件

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  7. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  8. java读取.properties文件

    在web开发过程中,有些配置要保存到properties文件里,本章将给出一个工具类,用来方便读取properties文件. 案例: 1:config.properties文件 name=\u843D ...

  9. Java 读取Properties文件时应注意的路径问题

    1. 使用Class的getResourceAsStream()方法读取Properties文件(资源文件)的路径问题:  InputStream in = this.getClass().getRe ...

随机推荐

  1. Octave 软件的安装

    每次安装软件都感觉是一种心痛的历程.下载安装,然后就跳出一堆的错误,之后就各种百度求救,然后就搞了大半天,有时候还搞不定. 最后,搞定的时候发现,原来这么简单,结果时间就这样浪费了,所以还是把这个过程 ...

  2. Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)

    给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...

  3. Mathtype部分数学符号不能显示,只能显示方框时的解决办法

    解决方法:打开C:\WINDOWS\Fonts,若里面有MT Extra(TrueType)字体或其快捷方式,则将其删除,再把MathType安装目录下\MathType 6.0\Fonts\True ...

  4. 爬虫之robots.txt

    robots是网站跟爬虫间的协议,用简单直接的txt格式文本方式告诉对应的爬虫被允许的权限,也就是说robots.txt是搜索引擎中访问网站的时候要查看的第一个文件. 当一个搜索蜘蛛访问一个站点时,它 ...

  5. HDU4578 Transformation (多操作线段树)

    传送门 终于过了这道题.. 要注意标记之间的影响,和add操作时更新求和的顺序. same 区间每个数设置为x标记 mult  区间每个数乘x标记 add  区间每个数加x标记 ①:当打same标记时 ...

  6. JSP-Cookie和Session

    1 会话技术简介 1.1 存储客户端状态 1.2 会话技术 2 Cookie技术 2.1 Cookie技术的购物 2.2 服务器端向客户端发Cookie //1.创建cookie对象 Cookie c ...

  7. day18 10.使用ThreadLocal来解决问题

    ThreadLocal是一个容器/集合,是一个Map集合.不管你跨多少层,只要你是同一个线程就可以取出来.Service和Dao是同一个线程.Service第一次调用JdbcUtils.getConn ...

  8. NOIP模拟赛 6.29

    2017-6-29 NOIP模拟赛 Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模型.最新款自然有着与以往不同的功能,那就是它能够自动行走, ...

  9. Leetcode33.Search in Rotated Sorted Array搜索旋转排序数组

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值, ...

  10. 小飞音箱wifi配网流程

    音箱出货时,已经内置wifi,如果无法接通,按照如下方案执行: 小飞音箱wifi配网流程 0. 接通音箱电源 通电3分钟后,音箱如果显示红色光圈,表示未联网,则需要手动联网 1. 手机下载小飞在线ap ...