操作properties文件,注意抹掉最前面的"file:"
package com.xiewanzhi.property;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties; public class PropertiesConfig { /**
* 根据KEY,读取文件对应的值
* @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties
* @param key 键
* @return key对应的值
*/
public static String readData(String filePath, String key) {
//获取绝对路径
filePath = PropertiesConfig.class.getResource("/" + filePath).toString();
//截掉路径的”file:“前缀
filePath = filePath.substring();
Properties props = new Properties();
try {
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
in.close();
String value = props.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
} public static void writeData(String filePath, String key, String value) {
//获取绝对路径
filePath = PropertiesConfig.class.getResource("/" + filePath).toString();
//截掉路径的”file:/“前缀
filePath = filePath.substring();
Properties prop = new Properties();
try {
File file = new File(filePath);
if (!file.exists())
file.createNewFile();
InputStream fis = new FileInputStream(file);
prop.load(fis);
//一定要在修改值之前关闭fis
fis.close();
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(key, value);
//保存,并加入注释
prop.store(fos, "Update '" + key + "' value");
fos.close();
} catch (IOException e) {
System.err.println("Visit " + filePath + " for updating " + value + " value error");
}
} public static void main(String[] args) {
System.out.println(PropertiesConfig.readData("com/xiewanzhi/property/config.properties", "port"));
// PropertiesConfig.writeData("com/xiewanzhi/property/config.properties", "port", "12345");
}
}
操作properties文件,注意抹掉最前面的"file:"的更多相关文章
- JAVA操作properties文件
va中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties ...
- Java代码操作properties文件(读取,新增/修改,删除)
项目中需要用到操作properties文件中的数据,记录一下 package com.bonc.savepic.save; import java.io.FileNotFoundException; ...
- Java学习笔记(二)——Java操作properties文件
[前面的话] 前段时间在学习和玩java web相关的东西,对于这些技术,一边学习,一边做东西,一边总结,希望可以一边成长和有所收获.有时总是思考太多反而成为了前进的阻力,所以对于生活还是简单一些,不 ...
- JAVA使用和操作properties文件
java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properti ...
- Java读取修改Properties文件
properties文件是我们经常需要操作一种文件,它使用一种键值对的形式来保存属性集. 无论在学习上还是工作上经常需要读取,修改,删除properties文件里面的属性. 本文通过操作一个prope ...
- Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作
有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn. ...
- Java之properties文件读取
1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
随机推荐
- 第20章 使用LNMP架构部署动态网站环境
章节概述: 本章节将从Linux系统的软件安装方式讲起,带领读者分辨RPM软件包与源码安装的区别.并能够理解它们的优缺点. Nginx是一款相当优秀的用于部署动态网站的服务程序,Nginx具有不错的稳 ...
- Linux下使用fdisk扩展分区容量
导读 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,比如:共享文件服务器硬盘空间不足,在这个时候我们就需要增加磁盘空间,来满足线上的业务:又或者我们在使用linux的过程中, 有时 ...
- 专注docker安全:Security Scanning
导读 Docker毫无疑问是近期运维同学们的热点话题,Docker安全也由此倍受重视,Docker Security Scanning 是一款Docker镜像扫描的安全工具,目前已经在Docker C ...
- POJ 1797 Heavy Transportation (Dijkstra变形)
F - Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- Step
php+MySQL html+css JQuery Mobile JavaScript weiPHP Sina Cloud 微信公众订阅号平台开发
- dom 中事件
阻止表单提交: function aa(){ return false; } function bb(event){ event.preventDefault(); } 事件不再派发: <!DO ...
- iOS 中使用类别简化代码开发
最近需要一个函数,把CLLocation对象转化为NSDictionary,按照我以前的想法,我会写一个工具类,之后添加一个函数,类似这样 - (NSDictionary *)turnLocation ...
- C#模拟百度登录
目录: 1.fiddler解析百度登录地址 2.处理传入参数 1.fiddler解析百度登录地址 因工作需要,所以研究了下百度的登陆.首先打开https://passport.baidu.com/v2 ...
- Java for LeetCode 201 Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- August 5th, 2016, Week 32nd, Friday
Life is made up of small pleasures. 生活由各种细小的幸福构成. Don't expect too much. I am not qualified to get m ...