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

先附上代码吧:

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. 操作系统 | 结合 CPU 理解一行 Java 代码是怎么执行的

    根据冯·诺依曼思想,计算机采用二进制作为数制基础,必须包含:运算器.控制器.存储设备,以及输入输出设备,如下图所示. 我们先来分析 CPU 的工作原理,现代 CPU 芯片中大都集成了,控制单元,运算单 ...

  2. PCL中有哪些可用的PointT类型(3)

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=268 PointXYZRGBNormal - float x, y, z, ...

  3. Base64加密 解密

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. Docker 面试题

    Docker 面试题 Docker? Docker是一个容器化平台,它以容器的形式将您的应用程序及其所有依赖项打包在一起,以确保您的应用程序在任何环境中无缝运行. CI(持续集成)服务器的功能是什么? ...

  5. SpringBoot使用AOP获取请求参数

    最近试了下拦截controller方法查看请求参数,方式如下: package com.*.aop; import com.alibaba.fastjson.JSON; import com.alib ...

  6. 【AMAD】django-allauth

    简介 个人评分 简介 django-allauth1集成的Oauth API包括: Amazon (OAuth2) AngelList (OAuth2) Bitly (OAuth2) Dropbox ...

  7. Linux系统封装成iso文件

    #安装所需软件包yum -y install createrepo mkisofs anaconda-runtime 根据root下的install.log文件,得到安装的软件包awk '/Insta ...

  8. 基于vue-cli项目打包慢的定位优化过程

    入职一周后,上一个前端就离职了(超级坑爹的),留下了一个比较棘手的问题,就是基于vue-cli的项目打包超级慢,我接手项目的时候,打包需要45min(上个离职者也不知道原因),经过3个月之后,随着项目 ...

  9. poj3122 Pie (二分)

    题目链接:https://vjudge.net/problem/POJ-3122 题意:有n块饼,m+1个人,将饼均分成m+1块,求每块最大的大小. 思路:水二分,显然每块的大小与可以给多少人吃具有单 ...

  10. Linux下的I/O复用与epoll详解(转载)

    Linux下的I/O复用与epoll详解 转载自:https://www.cnblogs.com/lojunren/p/3856290.html  前言 I/O多路复用有很多种实现.在linux上,2 ...