1.读取项目内的properties文件,项目内的properties文件一般都放在resource文件夹下面,

通过getClassLoader().getResourceAsStream()来获取取InputStream。

代码如下:

    Properties    props = new Properties();
String PATH="jdbc.properties";
InputStream in = PropertyTest.class.getClassLoader().getResourceAsStream(PATH);
props.load(in);
String value=props.getProperty("user");

2.读取指定路径的properties文件,通过BufferedInputStream来获取流。这种方法,可以获取项目工程外的properties文件。

代码如下:

  Properties    props = new Properties();
String PATH="E:\\JavaDemo\\src\\main\\resource\\jdbc.properties";
InputStream in = new BufferedInputStream(new FileInputStream( PATH ));
props.load(in);
String value=props.getProperty("user");

3.常用的工具类如下所示:

public class PropertyUtil {
//将要读取的properties的文件名
private static String propertiesFileName="jdbc.properties";
private static final Logger logger = Logger.getLogger(PropertyUtil.class);
private static Properties props= new Properties();
static{
logger.info("执行静态代码块loadProps(),保存在jvm中,避免多次执行。");
loadProps();
} synchronized static private void loadProps(){
logger.info("开始加载properties文件内容.......");
InputStream in = null;
try {
//       <!--第一种,通过类加载器进行获取properties文件流,路径为相对路径-->
in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertiesFileName);
//       <!--第二种,通过类进行获取properties文件流-->
//in = PropertyUtil.class.getResourceAsStream("propertiesFileName");
props.load(in);
} catch (FileNotFoundException e) {
logger.error("properties文件未找到");
} catch (IOException e) {
logger.error("出现IOException");
} finally {
try {
if(null != in) {
in.close();
}
} catch (IOException e) {
logger.error("properties文件流关闭出现异常");
}
}
logger.info("加载properties文件内容完成...........");
} public static String getProperty(String key){
if(null == props) {
loadProps();
}
return props.getProperty(key);
} public static String getProperty(String key, String defaultValue) {
if(null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}

读取指定路径的Properties文件的更多相关文章

  1. Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)

    Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://b ...

  2. iOS案例:读取指定目录下的文件列表

    // // main.m // 读取指定目录下的文件列表 // // Created by Apple on 15/11/24. // Copyright © 2015年 Apple. All rig ...

  3. 初识TypeScript:查找指定路径下的文件按类型生成json

    如果开发过node.js的话应该对js(javascript)非常熟悉,TypeScript(以下简称ts)是js的超集. 下面是ts的官网: https://www.tslang.cn/ 1.环境配 ...

  4. matlab读取指定路径下的图像

    利用matlab读取指定路径下的图像 %% 读入指定路径imgFolder下的图像imgName imgFolder = 'F:\博\快盘\图像+数据\images\文章实验图'; %指定路径 img ...

  5. File操作-将数据库里的数据写入到指定路径的txt文件里

    package com.Cristin.File;//将数据库里的数据写入到指定路径的txt文件里 import java.io.File;import java.io.FileOutputStrea ...

  6. Python获取指定路径下所有文件的绝对路径

    需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...

  7. 前端上传图片回显并用base64编码,后端做解码储存,存储图片路径在.properties文件中配置(以上传身份证照片为例)

    前端页面:<form id="kycForm" enctype="multipart/form-data"> <input type=&quo ...

  8. idea指定SpringBoot启动.properties文件

    比如我的项目下有2个.properties文件,一个是application.properties,一个是application-local.properties,在本地的时候想指定用applicat ...

  9. node.js+react全栈实践-Form中按照指定路径上传文件并

    书接上回,讲到“使用同一个新增弹框”中有未解决的问题,比如复杂的字段,文件,图片上传,这一篇就解决文件上传的问题.这里的场景是在新增弹出框中要上传一个图片,并且这个上传组件放在一个Form中,和其他文 ...

随机推荐

  1. HDU 2485

    http://acm.hdu.edu.cn/showproblem.php?pid=2485 n个车站,m条边,两边之间费用为1,问最少摧毁多少车站,使得1-n无法在k时间内到达 将2-(n-1)每个 ...

  2. CF1143D/1142A The Beatles

    CF1143D/1142A The Beatles 将题目中所给条件用同余方程表示,可得 \(s-1\equiv \pm a,s+l-1\equiv \pm b\mod k\). 于是可得 \(l\e ...

  3. python和C语言互相调用的几种方式

    ? 1 2 3 4 5 6 7 8 9 版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址   http://www.cnblogs.com/Colin-Cai/ ...

  4. Mysql中谓词使用date_format的优化

    优化前: SELECT a.* FROM t1 a, (SELECT obj_id,MAX(PRE_DETAIL_INST_ID) PRE_DETAIL_INST_ID FROM t1 WHERE D ...

  5. IAR 9+ 编译 TI CC2541 出现 Segment ISTACK (size: 0xc0 align: 0) is too long for segment definition.

    IAR 9+ 编译 TI CC2541 出现 Segment ISTACK (size: 0xc0 align: 0) is too long for segment definition. Segm ...

  6. 记录tomcat的完整日志

    Tomcat报的错太含糊了,什么错都没报出来,只提示了Error listenerStart.为了调试,我们要获得更详细的日志.可以在WEB-INF/classes目录下新建一个文件叫logging. ...

  7. C语言 字符串处理函数 转自 http://blog.chinaunix.net/uid-25885064-id-3175049.html

     C字符串处理函数 2012-04-13 18:14:16 分类: C/C++ void *memccpy (void *dest, const void *src, int c, size_t n) ...

  8. USACO 2016 January Contest, Gold解题报告

    1.Angry Cows http://www.usaco.org/index.php?page=viewproblem2&cpid=597 dp题+vector数组运用 将从左向右与从右向左 ...

  9. 基于nginx-rtmp-module模块实现的HTTP-FLV直播模块(nginx-http-flv-module)

    本文后续的内容将在这里更新:<基于nginx-rtmp-module模块实现的HTTP-FLV直播模块(nginx-http-flv-module)续>.注意:下文的配置很多已经不能用了, ...

  10. JVM体系结构之七:持久代、元空间(Metaspace) 常量池==了解String类的intern()方法、常量池介绍、常量池从Perm-->Heap

    一.intern()定义及使用 相信绝大多数的人不会去用String类的intern方法,打开String类的源码发现这是一个本地方法,定义如下: public native String inter ...