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:"的更多相关文章

  1. JAVA操作properties文件

    va中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties ...

  2. Java代码操作properties文件(读取,新增/修改,删除)

    项目中需要用到操作properties文件中的数据,记录一下 package com.bonc.savepic.save; import java.io.FileNotFoundException; ...

  3. Java学习笔记(二)——Java操作properties文件

    [前面的话] 前段时间在学习和玩java web相关的东西,对于这些技术,一边学习,一边做东西,一边总结,希望可以一边成长和有所收获.有时总是思考太多反而成为了前进的阻力,所以对于生活还是简单一些,不 ...

  4. JAVA使用和操作properties文件

    java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properti ...

  5. Java读取修改Properties文件

    properties文件是我们经常需要操作一种文件,它使用一种键值对的形式来保存属性集. 无论在学习上还是工作上经常需要读取,修改,删除properties文件里面的属性. 本文通过操作一个prope ...

  6. Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作

    有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn. ...

  7. Java之properties文件读取

    1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...

  8. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  9. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

随机推荐

  1. unity3d AssetBundle包加密

    原地址:http://www.cnblogs.com/88999660/archive/2013/03/15/2961587.html 保护资源管理文件的相关内容 Unity允许用户使用AssetBu ...

  2. move 和 CopyMemory的区别

    Move(ABuffer,P, Sizeof(ABuffer));   //指针传递            Move(ABuffer^,P^, Sizeof(TArrayByte));   //复制内 ...

  3. C语言 单引号和双引号的区别

    最近的C语言课在教字符串,貌似N多同学搞不清楚单引号和双引号的区别,有人还以为在C语言里用哪个都可以...其实C语言中的单引号和双引号含义是一点也不一样滴... 1.含义不同. 用单引号引起的一个字符 ...

  4. 使用virtualenv搭建独立的Python环境

    virtualenv可以搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题. 一.安装virtualenv virtualenv实际上是一个pyth ...

  5. 【leetcode】3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  6. 数据库路由器 ICX

    实时并发数据库事务处理同步复制器和负载平衡器    ———通向真正数据库高可用性,高可靠性,高性能之路 一.产品概述    数据库路由器--ICX是美国宾夕法尼亚大学计算机系施教授经过多年研究.开发出 ...

  7. Windows下尝试PHP7提示丢失VCRUNTIME140.DLL的问题解决

    前天PHP7.0.0正式版发布了,有一些比较好的改进,官方也说速度比php5.6快了两倍,性能上有了很大提升,并且也发布了从php5.x向php7迁移的问题,所以今后php网站迁移后能够大幅度的提升网 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. php的socket通信(一)

    什么是TCP/IP.UDP? TCP/IP(Transmission Control Protocol/Internet Protocol)即传输控制协议/网间协议,是一个工业标准的协议集,它是为广域 ...

  10. 【转】一个不错的eclipse反编译插件

    [转]一个不错的eclipse反编译插件 在CSDN论坛上看到的一个不错的eclipse反编译插件,感觉看起来不错的样子,因而记下,原网址是:http://topic.csdn.net/u/20121 ...