package jp.co.test.common.utils;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
public class PropertyUtil {
//根据key读取value
public static String readValue(String filePath, String key) {
Properties props = new Properties();
try {
InputStream in =
new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
String value = props.getProperty(key);
System.out.println(key + value);
return value;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//读取properties的全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream in =
new BufferedInputStream(new FileInputStream(filePath));
props.load(in);
@SuppressWarnings("rawtypes")
Enumeration en = props.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = props.getProperty(key);
System.out.println(key + Property);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 写入properties信息
public static void writeProperties(String filePath, String parameterName,
String parameterValue) {
Properties prop = new Properties();
try {
InputStream fis = new FileInputStream(filePath);
//从输入流中读取属性列表(键和元素对)
prop.load(fis);
//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
//强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(filePath);
prop.setProperty(parameterName, parameterValue);
//以适合使用 load 方法加载到 Properties 表中的格式,
//将此 Properties 表中的属性列表(键和元素对)写入输出流
prop.store(fos, "Update '" + parameterName + "' value");
} catch (IOException e) {
System.err.println("Visit " + filePath + " for updating "
+ parameterName + " value error");
}
}
public static void main(String[] args) {
readValue("info.properties", "url");
writeProperties("info.properties", "age", "21");
readProperties("info.properties");
System.out.println("OK");
}
}
分享到:
相关推荐
利用Java的Properties 类读取配置文件信息! 读取Properties文件的例子
java读取properties文件的工具类,传入配置文件名字和其中的key就可以读取
java如何读取properties配置文件
在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们...JDK 内置的java.util.Properties 类为我们操作.properties 文件提供了便利。
使用Java中的Properties类的方法,以及类加载器来读取配置文件中的信息。可以读取.properties文件和.cfg文件。详细代码+注释+测试例子
自己封装的JDBC连接通过properties文件读取配置文件
读取properties文件返回map
这是做项目中,集成第三东西需要些配置,我抽取到属性文件中,然后写了个公用类统一调用,基本就是key返回具体值,属性文件位置自己可以调整,然后类中修改获取路径就可以了。目前我的属相文件放置在src下
主要介绍了java简单读取properties配置文件的方法,涉及java针对properties配置的载入及文件属性读取相关操作技巧,需要的朋友可以参考下
java中properties文件读取demo
NULL 博文链接:https://bijian1013.iteye.com/blog/2307358
像Python支持的配置文件是.ini文件,同样,它也有自己读取配置文件的类ConfigParse,方便程序员或用户通过该类的方法来修改.ini配置文件。在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的...
java项目读取配置文件的实例代码,使用class.getClassLoader().getResourceAsStream方法。
大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...
工具类里分读取.yml工具类和.properties工具类,结合博客描述使用,用不到的方法可根据个人情况删除,
NULL 博文链接:https://liuzidong.iteye.com/blog/776637
ConfigFile configfile = ConfigFile.getInstance("ipConfig123.properties"); String ip = configfile.getkeyvalue("ip"); 可以取出ipConfig123.properties 文件中IP的内容
Java配置文件读取,采用单例模式加载config.properties
NULL 博文链接:https://jeemygrow.iteye.com/blog/1162827
NULL 博文链接:https://z-gxjs.iteye.com/blog/1846423