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 ...
随机推荐
- Linux終端一行命令发送邮件
近期由于经常需要给别人发送邮件,每次都要打开QQ邮箱觉得非常麻烦.想到Linux终端可以自定义命令,加上python可以实现邮件发送功能,于是自己写了一个终端send + 文件地址的命令. 首先贴上p ...
- vscode中LaTeX的编写
前言 在学习\(\mathrm{\LaTeX}\)的时候尝试过很多编辑器,但都被其复古的外观或者复杂的配置劝退.并且因为本身就在使用VScode写其他的一些语言,正好借此机会也学习一下怎么用VScod ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box
#include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...
- TODO:如何模拟cpu打满,磁盘打满,网卡打满
背景: 测试活动中,需要构造cpu打满.磁盘打满.网卡打满的场景 场景1:cpu打满 环境信息: 虚拟机,物理核数16个,每个物理核的虚拟核数1个,虚拟核数16个: [root@vm10-0-0-8 ...
- 用户登录(php)
<!DOCTYPE HTML><html><head><meta charset="utf-8"><script type=& ...
- javascript 循环读取数组中的值
//数组 var a = ["#F85C6F", "#78B0F0", "#DB83ED", "#8EC656", &q ...
- util之PriorityQueue
定义: PriorityQueue<Integer> queue = new PriorityQueue<Integer>(); java中的优先队列默认从小到大 //自定义 ...
- scss(sass)
- 查看ie版本
window10系统还好说,一般都是IE11版本,其他系统或服务器看=查看ie版本就很烦 方法一:按组合键 ALT+H 打开“帮助”,再按 A 选择“关于Internet Explorer” 方法二 ...
- 自动化脚本-配置LVS(DR模式)
一,获取所需配置的主机IP cat get_ip.sh #!/bin/bash #将获得到的IP地址写进IP.txt >ip.txt #将原有的hosts信息清除 >/root/.ssh/ ...