1。使用java.util.Properties类的load()方法
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name)); Properties p = new Properties();
p.load(in);
2。使用java.util.ResourceBundle类的getBundle()方法
示例:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());
3。使用java.util.PropertyResourceBundle类的构造函数
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in);
4。使用class变量的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in);
5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in);
6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in);
7.Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);
如何从输入流中加载属性文件
使用load(InputStream is)方法:
Properties properties = new Properties();
InputStream is = new FileInputStream("conn.properties");
properties.load(is);
is.close();
如何读属性文件中的值
使用getProperties(String key)方法:
String temp = properties.getProperties(String key);
<注>重载的方法getProperties(String key, String default)方法 将在查询不到值的情况下,返回default.
即: 如果 null == properties.getProperties(String key);
则有 default == properties.getProperties(String key, String default);
如何获取属性文件中的所有的键
使用propertyNames()方法,该方法返回是键的枚举.
Enumeration enumeration = properties.propertyNames();
如何修改属性文件中的值
setProperties(String key, String value)
方法.
<注>该方法调用的 Hashtable 的put方法.如果键存在,则修改值;如果键不存在,则添加值.
如何存储属性文件到输出流
使用store(OutputStream os, String description)方法:
Properties properties = new Properties();
OutputStream os = new FileOutputStream("test.properties");
String description = "store properties to test.properties";
properties.store(os, description);
os.close();
如何清空所有值
使用
clear()
方法.
<注>该方法继承自 Hashtable 的clear()方法.清空哈希表.
三.实例附件
<注>实例中没有指明properties文件的绝对路径.那么默认是在项目根目录下的.
当生成新文件时,使用F5刷新就能看见新文件产生了.
分享到:
相关推荐
主要介绍了Python实现读取Properties配置文件的方法,结合实例形式分析了Python读取Properties配置文件类的定义与使用相关操作技巧,需要的朋友可以参考下
properties文件读写操作
c#操作properties,读写配置文件,非常方便,几行代码就搞定
很全面 很有用。。。里面有对Properties文件进行读取,写入的操作,达到六种方法。。。
因为他内部有一个ConfigParser 类来支持.ini 文件的读写,根据该类提供的方法程序员可以自由的来操作.ini 文件。而在Java 中,Java 支持的是.properties 文件的读写。JDK 内置的java.util.Properties 类为我们操作....
java操作config.properties方法,提供了操作txt、xml、properties文件读写方法
主要介绍了java简单读取properties配置文件的方法,涉及java针对properties配置的载入及文件属性读取相关操作技巧,需要的朋友可以参考下
主要介绍了java读取properties文件的方法,实例分析了java操作properties配置文件的相关技巧,需要的朋友可以参考下
主要介绍了java读取properties配置文件的方法,涉及java操作properties配置文件的相关技巧,需要的朋友可以参考下
读取配置文件properties的内容,提供信息的读取操作
/*读操作 CProperties cprop; PSTATUS ret = cprop.open("D:\\job\\greatwall\\test\\bank.properties"); if (ret != IS_OK) { cout打开配置文件失败"; getchar(); return 0; } cprop.load(); vector<string> vec...
主要介绍了Python读取properties配置文件操作,结合实例形式分析了Python读取properties配置文件操作中增加对键值对支持与使用的相关技巧,需要的朋友可以参考下
主要介绍了java使用POI读取properties文件并写到Excel的方法,涉及java操作properties文件及Excel文件的相关技巧,需要的朋友可以参考下
一般文件以及配置文件的读写操作: 包括一般text文件的读取,以及properties文件的读写操作
properties文件读取操作,方法按模块划分,方便开发使用
java.util.Properties 这个类,可以很方便的处理properties 文件, 在python 中虽然也有读取配置文件的类ConfigParser, 但如果习惯java 编程的人估计更喜欢下面这个用python 实现的读取 properties 文件的类: ...
主要介绍了Java读取properties配置文件的方法讲解,properties可以被看作是Java世界的ini,Java中有Properties可以操作它,需要的朋友可以参考下