前几天开发时遇到一个问题,在后台读取配置文件的时候无法读取属性值,于是上网查了查,现在在这分享给大家;

先附上代码吧:

package com.shafei.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.log4j.Logger;
public class PropertieUtil {
private static Logger logger = Logger.getLogger(PropertieUtil.class);
private PropertieUtil() {
}
/**
* 读取配置文件某属性
*/
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
// 注意路径以 / 开始,没有则处理
if (!filePath.startsWith("/"))
filePath = "/" + filePath;
InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception e) {
logger.error(e);
return null;
}
}
/**
* 打印配置文件全部内容(filePath,配置文件名,如果有路径,props/test.properties)
*/
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
// 注意路径以 / 开始,没有则处理
if (!filePath.startsWith("/"))
filePath = "/" + filePath;
InputStream in = PropertieUtil.class.getResourceAsStream(filePath);
props.load(in);
Enumeration<?> en = props.propertyNames();
// 遍历打印
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
System.out.println(Property);
logger.info(key + ":" + Property);
}
} catch (Exception e) {
logger.error(e);
}
}
/**
* 将值写入配置文件
*/
public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
// 本地测试特别注意,如果是maven项目,请到\target目录下查看文件,而不是源代码下
// 注意路径不能加 / 了,加了则移除掉
if (fileName.startsWith("/"))
fileName.substring(1);
String filePath = PropertieUtil.class.getResource("/").getPath()+fileName;
// 获取配置文件
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
in.close();
OutputStream out = new FileOutputStream(filePath);
// 设置配置名称和值
pps.setProperty(parameterName, parameterValue);
// comments 等于配置文件的注释
pps.store(out, "Update " + parameterName + " name");
out.flush();
out.close();
}
public static void main(String[] args) throws Exception {
readProperties("jdbc.properties");
logger.info(readValue("jdbc.properties", "JAVABLOG_WRITE_URL"));
// writeProperties("conf/test.properties", "dataSource.driver", "test");
readProperties("conf/test.properties");
}
}

上面的代码我也是学习大牛们的,也不是很难,很实用,希望对大家有帮助!

java后台读取配置文件的更多相关文章

  1. java后台读取配置文件中key与value -----demo2

    /** * * @Title: getValue * @Description: TODO * @param key * @return import java.util.Properties; * ...

  2. java后台读取配置文件中key与value -----demo

    public class ResourcesUtils { /* * @description:根据属性获取文件名 * * @param:propertyName文件的属性名 * * @return: ...

  3. java中读取配置文件ResourceBundle和Properties两种方式比较

    今天在开发的时候,需要把一些信息放到配置文件中,方便后续的修改,注意到用的是ResourceBundle读取配置文件的方式,记得之前也见过使用Properties的方式,就比较好奇这两种方式的区别,网 ...

  4. Java J2EE读取配置文件

    package com; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.naming.InitialCon ...

  5. java web 读取配置文件两种方法

    package com.tsinghua.getDataBaseConn; import java.io.IOException;import java.io.InputStream;import j ...

  6. Java中读取配置文件中的内容,并将其赋值给静态变量的方法

    应用场景 项目开发中某个功能需要抽取成方法写成一个工具类,提供给别人使用.写过工具类的人都知道,工具类中的方法一般都是静态方法,可以直接使用类名点方法名调用, 使用很方便,比如判断某个对象是否为空的方 ...

  7. 在java中读取配置文件信息

    public class PropertyUtil { public static final Properties PROP = new Properties(); /** * 读取配置文件的内容( ...

  8. java中读取配置文件的方法

    转自:http://blog.csdn.net/stypace/article/details/38414871 一.使用org.apache.commons.configuration 需要使用的是 ...

  9. 转载:Java项目读取配置文件时,FileNotFoundException 系统找不到指定的文件,System.getProperty("user.dir")的理解

    唉,读取个文件,也就是在项目里面去获得配置文件的目录,然后,变成文件,有事没事,总是出个 FileNotFoundException  系统找不到指定的文件,气死人啦. 还有就是:System.get ...

随机推荐

  1. C++ STL——deque

    目录 一 deque容器 1.1 deque容器基本概念 1.2 deque构造函数 1.3 deque赋值操作 1.4 deque大小操作 1.5 deque双端插入和删除操作 1.6 deque数 ...

  2. LC 216. Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java相关框架概念以及思想

    1.什么是IoC Ioc—Inversion of Control,即“控制反转”,是一种思想, 一个重要的面向对象编程的法则,它能指导我们如何设计出松耦合.更优良的程序. 高内聚低耦合的设计能够让构 ...

  4. Python统计分析可视化库seaborn(相关性图,变量分布图,箱线图等等)

    Visualization of seaborn  seaborn[1]是一个建立在matplot之上,可用于制作丰富和非常具有吸引力统计图形的Python库.Seaborn库旨在将可视化作为探索和理 ...

  5. R语言 我要如何开始R语言_数据分析师

    R语言 我要如何开始R语言_数据分析师 我要如何开始R语言? 很多时候,我们的老板跟我们说,这个东西你用R语言去算吧,Oh,My god!什么是R语言?我要怎么开始呢? 其实回答这个问题很简单,首先, ...

  6. Junit单元测试学习

    一.首先选择学习工具是IDEA 1>官网下载IDEA 1:官网地址IntelliJ IDEA,官网上对于不同的操作系统(windows,macOS,Linux)都有两个版本可供下载| 其中蓝色下 ...

  7. Python C/S架构,网络通信相关名词,socket编程

    主要内容: 一.  C/S架构 二. 网络通信的相关名词 三. socket编程 一. C/S架构和B\S架构概述 1. C/S架构: Client/Server(客户端/服务端)架构 描述: C/S ...

  8. jmeter-抓取数据库数据,循环请求1000次(变量嵌套变量)

    场景:数据库返回1000个数据,用这1000个数据去做请求 数据库请求结果为: 添加while控制器 添加BeanShell 预处理程序,${__counter(FALSE,Vcount)} 为自增函 ...

  9. WebContent下新建目录放入jsp,跳转servlet页面出错解决

    为方便分类jsp文件,于是在web-content下新建了一级目录,将jsp文件放入其中,结果原本跳转的servlet出现404错误: 解决如下: 在用eclipse创建的servle会自动生成一个注 ...

  10. android带参和不带参的页面跳转形式

    FActivity.java package com.example.demo06; import android.app.Activity;import android.content.Contex ...