读取Properties文件工具类
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文件工具类的更多相关文章
- Java读取properties文件工具类并解决控制台中文乱码
1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationCo ...
- 读取Config文件工具类 PropertiesConfig.java
package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...
- Property工具类,Properties文件工具类,PropertiesUtils工具类
Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...
- Java读取properties配置文件工具类
1. PropertyUtils.java package javax.utils; import java.io.InputStream; import java.util.Properties ...
- Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作
有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn. ...
- 加载Properties文件工具类:LoadConfig
import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; impor ...
- java读取properties文件工具
public class PropertiesUtil { public static String get(String filePath, String key) { String val = n ...
- java读取properties的工具类PropertiesUtil
package org.properties.util; import java.io.FileInputStream; import java.io.FileOutputStream; import ...
- java读取.txt文件工具类FileUtiles
public class FileUtils { private static final String ENCODING = "UTF-8";//编码方式 /** * 获取文件的 ...
随机推荐
- 贴代码—CF230 DIV1 B
题目在此: http://codeforces.com/contest/392/problem/B 一直理解错了一句话,以为是用最小的move求最小的花费, 读错题目的有木有!!! 不懂汉诺塔的原理有 ...
- zend studio插件
1.安装使用Aptana插件(html,css,js代码提示功能) 安装步骤: 1).zend studio->Help->Install New Software->work wi ...
- 7 天玩转 ASP.NET MVC — 第 7 天
目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 今天是开心的一天.因为我们终于来到了系列学习的最后一节.我相信你喜欢之前的课程,并从中学到了许多. ...
- cf div2 239 D
D. Long Path time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- java_String和StringBuffer区别分析
JAVA平台提供了两个类:String和StringBuffer,它们可以储存和操作字符串,即包含多个字符的字符数据.这个String类提供了数值不可改变的字符串.而这个StringBuffer类提供 ...
- QString::toWCharArray可以拷贝到宽字符串里
wchar_t * sourcepath=new wchar_t[MAX_PATH];int s1=str.toWCharArray(sourcepath); sourcepath[s1]=0;
- Github 学习
1.git$cd ~/hello-world.$git add . //这样可以自动判断新加了哪些文件,或者手动加入文件名字$git commit //提交到本地仓库,不加参数会提示,注意:^=Ctr ...
- JMS基本概念
原文:http://blog.csdn.net/jiuqiyuliang/article/details/46701559 The Java Message Service (JMS) API is ...
- CentOS 7 中firewall-cmd命令
在 CentOS 7 暂时开放 ftp 服务# firewall-cmd --add-service=ftp 永久开放 ftp 服务# firewall-cmd --add-service=ftp - ...