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中定时器的使用

    源地址:http://unity3d.9tech.cn/news/2014/0402/40149.html 在游戏设计过程中定时器是必不可少的工具,我们知道update方法是MonoBehavior中 ...

  2. 在 OpenGL ES 2.0 上实现视差贴图(Parallax Mapping)

    在 OpenGL ES 2.0 上实现视差贴图(Parallax Mapping) 视差贴图 最近一直在研究如何在我的 iPad 2(只支持 OpenGL ES 2.0, 不支持 3.0) 上实现 视 ...

  3. C/C++语法知识:typedef struct 用法详解

    第一篇:typedef struct与struct的区别 1. 基本解释 typedef为C语言的关键字,作用是为一种数据类型定义一个新名字.这里的数据类型包括内部数据类型(int,char等)和自定 ...

  4. 应用 JD-Eclipse 插件实现 RFT 中 .class 文件的反向编译

    概述 反编译是一个将目标代码转换成源代码的过程.而目标代码是一种用语言表示的代码 , 这种语言能通过实机或虚拟机直接执行.文本所要介绍的 JD-Eclipse 是一款反编译的开源软件,它是应用于 Ec ...

  5. Power of Two & Power of Three & Power of Four

     Check Power of 2 Using O(1) time to check whether an integer n is a power of 2. Example For n=4, re ...

  6. object-c 基本数据类型

    1.基本数据类型   int  float  double  char   布尔类型   枚举类型 2.对象类型和id类型  就是类类型或协议所声明的指针类型.  id类型可以表示任何类型,一般只表示 ...

  7. c/c++指针总结[pointer summary]

    [本文链接] http://www.cnblogs.com/hellogiser/p/pointer-summary.html 1.指针注意事项 (1). 指针类型字符串不容许修改 char *str ...

  8. mysql 表空间

    开启了Innodb的innodb_file_per_table这个参数之后[innodb_file_per_table = 1],也就是启用InnoDB的独立表空间模式,便于管理.此时,在新建的inn ...

  9. Java for LeetCode 024 Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  10. python——生成器

    python——生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个 ...