java读写Properties属性文件公用方法
Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件。
它提供了几个主要的方法:
1. getProperty ( String key),用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream inStream),从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 test.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String key) 来搜索。
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。
4. store ( OutputStream out, String comments),以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear (),清除所有装载的 键 - 值对。该方法在基类中提供。
package org.meter.demo.properties;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger; public class PropertiesTest { private static Logger logger = Logger.getLogger(PropertiesTest.class.getName());
private PropertiesTest() {
}
/**
* 读取配置文件某属性
*/
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
// 注意路径以 / 开始,没有则处理
if (!filePath.startsWith("/"))
filePath = "/" + filePath;
InputStream in = PropertiesTest.class.getResourceAsStream(filePath);
props.load(in);
String value = props.getProperty(key);
return value;
} catch (Exception e) { logger.log(Level.WARNING,e.getMessage());
return null;
}
}
/**
* 打印配置文件全部内容(filePath,配置文件名,如果有路径,props/test.properties)
*/
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
// 注意路径以 / 开始,没有则处理
if (!filePath.startsWith("/"))
filePath = "/" + filePath;
InputStream in = PropertiesTest.class.getResourceAsStream(filePath);
props.load(in);
Enumeration<?> en = props.propertyNames();
// 遍历打印
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
logger.log(Level.INFO, key + ":" + Property);
}
} catch (Exception e) {
logger.log(Level.WARNING, e.getMessage());
}
}
/**
* 将值写入配置文件
*/
public static void writeProperties(String fileName, String parameterName, String parameterValue) throws Exception {
// 本地测试特别注意,如果是maven项目,请到\target目录下查看文件,而不是源代码下
// 注意路径不能加 / 了,加了则移除掉
if (fileName.startsWith("/"))
fileName.substring(1);
String filePath = PropertiesTest.class.getResource("/").getPath()+fileName;
// 获取配置文件
Properties pps = new Properties();
File file = new File(filePath);
if(!file.exists()){
file.createNewFile();
}
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
in.close();
OutputStream out = new FileOutputStream(filePath); // 设置配置名称和值
pps.setProperty(parameterName, parameterValue);
// comments 等于配置文件的注释
pps.store(out, "Update " + parameterName + " name");
out.flush();
out.close();
}
public static void main(String[] args) throws Exception {
readProperties("org/meter/demo/properties/jdbc.properties");
writeProperties("org/meter/demo/properties/output.txt","test2","value2");
writeProperties("org/meter/demo/properties/output.txt","tesdes2","value");
}
}
java读写Properties属性文件公用方法的更多相关文章
- 使用JAVA读写Properties属性文件
使用JAVA读写Properties属性文件 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数 ...
- 解决读写properties属性文件
package com.kzkj.wx.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileIn ...
- java 读写properties (配置)文件
Properties属性文件在Java应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文 ...
- java下properties属性文件操作
package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...
- Code片段 : .properties属性文件操作工具类 & JSON工具类
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...
- Java学习笔记——JDBC读取properties属性文件
Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...
- java:Properties属性文件概念
java:Properties属性文件概念 在java之前的国际化程序中提出了一个属性文件的概念,属性文件的后缀是:*.properties,那么在java中提供了意个属性文件的专门操作类,Prope ...
- Java - 得到项目中properties属性文件中定义的属性值
public static String getPropertiesValue(String fileName, String key) { return ResourceBundle.getBu ...
- Java读取利用java.util类Properties读取resource下的properties属性文件
说明:upload.properties属性文件在resources下 import java.io.IOException;import java.io.InputStream;import jav ...
随机推荐
- 在Nodejs中如何调用C#的代码
最近需要在Nodejs中用到C#的代码,从网上了解到可以采用Edgejs来实现Nodejs与C#的代码交互, 直接复制网上的代码运行总是出各种错,填了不少坑,现在把自己的案例代码大致整理一下,方便以后 ...
- 分布式之ZookeeperMac安装
Zookeeper用于分布式计算中的协调员,观察员,观察者,分布式锁,在系统中利用Zookeeper来处理事件通知,队列,锁等功能. 使用brew安装 zookeeper. 可参考:http://bl ...
- response生成图片验证码
新建一个java web工程 src 目录下xieyuan包MyServlet.java文件(Servlet文件) package xieyuan; import java.awt.Color; im ...
- UE3名称结构(Name)
解释说明: (1) 直接通过FName的index进行比较来判断两个FName是否相等 (2) 通过FName的index从全局Names数组中取出对应的FNameEntry,可以获得FName的字符 ...
- C语言的可变参数在Linux(Ubuntu)与Windows下注意点
基本上C语言的可变参数原理在不同平台和不同编译器下基本类似(通过函数入栈,从右向左,从高位到低位地址),不过部分实现会有所不同:在使用中需要注意的是: va_list 为char 类型指针,部分调用如 ...
- Android功能点
1.获取屏幕尺寸.密度等信息. 1)最常用的方法: WindowManager windowManager = getWindowManager(); Display display = window ...
- fragment+viepager 的简单暴力的切换方式
这里是自定义了一个方法来获取viewpager private static ViewPager viewPager; public static ViewPager getMyViewPager() ...
- 关于handler 和 looper 的问题
重新去学习回顾looper和handler ,还是需要重新认识这个经常使用的机制. 我首先是看任玉刚老师的书<android的开发艺术探索>的第十章. 里面一句话开始说出了我们大概的理解— ...
- Android之获取数据库路径
如果数据库名为:“ifoData.db”,则它的路径求法为 String pathDatabase=Mcontext.getDatabasePath("ifoData.db").g ...
- 在VS2012下静态链接MFC的问题
1>------ 已启动生成: 项目: MFCApplication1, 配置: Debug Win32 ------1>uafxcwd.lib(afxctrlcontainer2.obj ...