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. 《JavaScript高级程序设计》

    第二章在html中使用Javascript2.1<script>在使用<script>嵌入JS代码时,不要再代码中的任何地方出现"</script>&qu ...

  2. javascript 最常用的技巧整理

    1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键<table border oncontextmenu= ...

  3. CSS 类名的单词连字符:下划线还是连接符?

    本文的部分内容整理自我对此问题的解答: 命名 CSS 的类或 ID 时单词间如何连接? - 知乎 问题 CSS 类或 ID 命名时单词间连接通常有这几种写法: 驼峰式: solutionTitle.s ...

  4. CodeForces 321A

    A. Ciel and Robot time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Ubuntu下开启ssh服务

    网上有很多介绍在Ubuntu下开启SSH服务的文章,但大多数介绍的方法测试后都不太理想,均不能实现远程登录到Ubuntu上,最后分析原因是都没有真正开启ssh-server服务.最终成功的方法如下: ...

  6. HDU 2671 Can't be easier(数学题,点关于直线对称)

    题目 //数学题//直线 y = k * x + b//直线 ax+by+c=0; 点 (x0,y0); 点到直线距离 d = (ax0+by0+c)/sqrt(a^2+b^2) /********* ...

  7. uva 11374

    Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...

  8. POJ 2265 Bee Maja (找规律)

    题目链接 题意 : 给你两个蜂巢的编号,给你一个的编号让你输出在另外一个蜂巢中对应的编号. 思路 : 先将蜂巢分层,第一层一个数,第二层6个数,第三层12个数…………然后用公式表示出第n层的最后一个数 ...

  9. 被 Windows 10 SDK 坑了

    抓包抓到的配置文件与完整版的VS一致,虽然显示出来的只有 Windows 10 通用工具,我还以为是只更新这些(因为列出来的几个,我确实之前没装),就愉快地点了安装,结果……新建项目时,发现 Wind ...

  10. sql server 2008 评估期已过期解决办法

    开始-->所有程序-->Microsoft SQL Server 2008-->配置工具-->SQL Server 安装中心-->维护-->版本升级,接着按照提示一 ...