import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.struts2.ServletActionContext; public final class ReadConfigFileUtil { private static Properties props = new Properties(); /* 在类初始化的时候加载配置文件 */
static {
// 1.读取配置文件
InputStream ins = ReadConfigFileUtil.class.getClassLoader().getResourceAsStream("config.properties");
try {
// 2.加载配置文件
props.load(ins);
} catch (Exception e) {
throw new RuntimeException(e + "【加载配置文件失败】");
} finally {
if (ins != null) {
try {
ins.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
} /**
* 根据给定的key键,读取器对应的值value
*
* @param key
* @return value
*/
public static String getValue(String key) {
try {
return props.getProperty(key) != null ? props.getProperty(key) : "";
} catch (Exception e) {
throw new RuntimeException(e + "读取属性【" + key + "】失败");
}
} /**
* 根据指定的
*
* @param key
* @param value
*/
public static void setValue(String key, String value) {
try {
props.setProperty(key, value);
} catch (Exception e) {
throw new RuntimeException(e + "为属性【" + key + "】,写入【" + value + "】失败");
}
} /**
* 保存修改
*
* @throws IOException
*/
public static void saveFile() throws IOException {
String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/classes/");
path += "\\config.properties";
FileOutputStream outputStream = new FileOutputStream(path);
props.store(outputStream, "配置文件");
outputStream.close(); }
}

读取Properties文件工具类的更多相关文章

  1. Java读取properties文件工具类并解决控制台中文乱码

    1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...

  2. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

  3. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  4. Java读取properties配置文件工具类

    1.   PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...

  5. Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作

    有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn. ...

  6. 加载Properties文件工具类:LoadConfig

    import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; impor ...

  7. java读取properties文件工具

    public class PropertiesUtil { public static String get(String filePath, String key) { String val = n ...

  8. java读取properties的工具类PropertiesUtil

    package org.properties.util; import java.io.FileInputStream; import java.io.FileOutputStream; import ...

  9. java读取.txt文件工具类FileUtiles

    public class FileUtils { private static final String ENCODING = "UTF-8";//编码方式 /** * 获取文件的 ...

随机推荐

  1. Sqli-labs less 22

    Less-22 本关和less20.less21是一致的,我们可以从源代码中看到这里对uname进行了"uname"的处理,可以构造payload: admin1"and ...

  2. 设置VMWARE通过桥接方式使用主机无线网卡上网

    原文:http://www.cnblogs.com/liongis/p/3265458.html 环境:WIN7旗舰版,台式机,U盘无线上网卡. 虚拟软件:VMware9.0,虚拟系统:CentOS6 ...

  3. (转)8 reviews about de novo genome assembly

    转自:http://dskernel.blogspot.com/2012/04/8-reviews-about-de-novo-genome-assembly.html 8 reviews about ...

  4. LA 4727

    Integers 1, 2, 3,..., n are placed on a circle in the increasing order as in the following figure. W ...

  5. Web App中的Flexbox应用

    虽然语法可能比较混杂,但 Flexbox 还是名不虚传的.它创造的是可伸缩的.有弹性的.可改变视觉顺序的智能盒子.它提供了简单的CSS布局方案范例让容器总是处于垂直水平居中的位置.使用盒模型来工作是非 ...

  6. POJ 1466

    #include<iostream> #include<stdio.h> #define MAXN 505 using namespace std; int edge[MAXN ...

  7. POJ 1538

    #include <iostream> #include <iomanip> using namespace std; ]; //拉格朗日插值算法 int main() { / ...

  8. ExtJs之Ext.core.Element

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  9. 从.NET 1.1 升级到.NET 4.0 遇到 线程间操作无效: 从不是创建控件 [XX] 的线程访问它.

    有两种方式解决 1.在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false;2.使用Invoke等委托函数 问题原因是 .NET2.0 以后拒绝 ...

  10. 简易解说拉格朗日对偶(Lagrange duality)(转载)

    引言:尝试用最简单易懂的描述解释清楚机器学习中会用到的拉格朗日对偶性知识,非科班出身,如有数学专业博友,望多提意见! 1.原始问题 假设是定义在上的连续可微函数(为什么要求连续可微呢,后面再说,这里不 ...