properties文件读写工具类PropertiesUtil.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties; /**
*
* @author
*
*/
public class PropertiesUtil { private String properiesName = ""; public PropertiesUtil() { } public PropertiesUtil(String fileName) {
this.properiesName = fileName;
} /**
* 按key获取值
* @param key
* @return
*/
public String readProperty(String key) {
String value = "";
InputStream is = null;
try {
is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
Properties p = new Properties();
p.load(is);
value = p.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return value;
} /**
* 获取整个配置信息
* @return
*/
public Properties getProperties() {
Properties p = new Properties();
InputStream is = null;
try {
is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
p.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return p;
} /**
* key-value写入配置文件
* @param key
* @param value
*/
public void writeProperty(String key, String value) {
InputStream is = null;
OutputStream os = null;
Properties p = new Properties();
try {
is = new FileInputStream(properiesName);
// is = PropertiesUtil.class.getClassLoader().getResourceAsStream(properiesName);
p.load(is);
// os = new FileOutputStream(PropertiesUtil.class.getClassLoader().getResource(properiesName).getFile());
os = new FileOutputStream(properiesName); p.setProperty(key, value);
p.store(os, key);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != is)
is.close();
if (null != os)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} } public static void main(String[] args) {
// sysConfig.properties(配置文件)
PropertiesUtil p = new PropertiesUtil("sysConfig.properties");
System.out.println(p.getProperties().get("db.url"));
System.out.println(p.readProperty("db.url"));
PropertiesUtil q = new PropertiesUtil("resources/sysConfig.properties");
q.writeProperty("myUtils", "wang");
System.exit(0);
} }
properties文件读写工具类PropertiesUtil.java的更多相关文章
- properties文件读写工具类
java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
- android 文件读写工具类
将可以序列化的对象通过base64编码后进行保存 但是感觉多数情况下,不需要采用这个功能,直接保存原始的json字符串,取出来之后再进行解析即可 package com.wotlab.home.mon ...
- list集合、txt文件对比的工具类和文件读写工具类
工作上经常会遇到处理大数据的问题,下面两个工具类,是在处理大数据时编写的:推荐的是使用map的方式处理两个list数据,如果遇到list相当大数据这个方法就起到了作用,当时处理了两个十万级的list, ...
- java文件读写工具类
依赖jar:commons-io.jar 1.写文件 // by FileUtilsList<String> lines = FileUtils.readLines(file, " ...
- properties文件读取工具类
项目中防止硬编码,经常会将一些与业务相关的数据存在properties文件中,根据不同的key加载不同的数据,所以就会有读取properties文件的东西,这篇文章仅为了以后copy方便写的. 1.添 ...
- java简单的文件读写工具类
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedRead ...
- 删除文件夹工具类 DeleteFolder.java
package com.util; import java.io.File; /** * 删除文件夹 * @createTime DSC 20, 2010 15:38 * @version 2.0 * ...
- Java-Properties文件读取工具类
import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configurat ...
随机推荐
- navicat异常 - 1130-host ... is not allowed to connect to this MySql server
错误描述 用navicat连接数据库报错:1130-host ... is not allowed to connect to this MySql server如何处理 解决方案 1.连接服务器: ...
- 835. 字符串统计(Trie树模板题)
维护一个字符串集合,支持两种操作: “I x”向集合中插入一个字符串x: “Q x”询问一个字符串在集合中出现了多少次. 共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母 ...
- 微信小程序块导航
1.wxml页面 <!--index.wxml--> <swiper current="{{currentTab}}" class="swiper-bo ...
- 3_time
3. Time and order What is order and why is it important? What do you mean "what is order"? ...
- pandas模块详解
Pandas模块 1.什么是pandas pandas是基于numpy构建的,用来做数据分析的 2.pandas能干什么 具备对其功能的数据结构DataFrame,Series 集成时间序列功能 提供 ...
- go语言 实现对称加密解密算法
package main import ( "bytes" "crypto/aes" "crypto/cipher" "crypt ...
- Ubuntu18.04 一条命令安装caffe问题
由于caffe安装坑很多,而且caffe框架很久不更新了,微调对框架影响不大,所以对与ubuntu18.04在caffe官网提供了一条命令安装,避免很多踩坑痛苦. CPU的一条命令安装: sudo a ...
- PP: Reconstructing time series into a complex network to assess the evolution dynamics of the correlations among energy prices
Purpose detect the dynamics in time series of their correlation Methodology 1. calculate correlation ...
- POJ2456 Aggressive cows(二分)
链接:http://poj.org/problem?id=2456 题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最 ...
- [AST Babel Plugin] Hanlde ArrowFunction && FunctionExpression
Continue with previous post: https://www.cnblogs.com/Answer1215/p/12342540.html Now we need to think ...