java中的Properties解释
程序员文章站
2022-06-08 15:41:10
...
Properties
1. 作用:读写资源配置文件
2. 键与值只能为字符串
3. 方法
setProperty(String key,String value)
getProperty(String key)
getProperty(String key,String defaultValue)
后缀:.properties
store(OutputStream out,String comment)
store(Writer writer, String comments)
load(InputStream inStream)
load(Reader reader)
.xml
storeToXML(OutputStream os,String comment) :UTF-8字符集
storeToXML(OutputStream os,String comment,String encoding)
loadFromXML(InputStream in)
package com.bjsxt.others.pro;
import java.util.Properties;
/**
* Properties 资源配置文件的读写
* 1、key 与value 只能为字符串
* 2、存储与读取
* setProperty(String key, String value)
* getProperty(String key, String defaultValue)
* @author Administrator
*
*/
public class Demo01 {
/**
* @param args
*/
public static void main(String[] args) {
//创建对象
Properties pro =new Properties();
//存储
pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
//pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
pro.setProperty("user", "scott");
pro.setProperty("pwd", "tiger");
//获取
String url =pro.getProperty("url","test");
//若所对应的值不存在,则用test代替
System.out.println(url);
}
}