properties配置文件
properties是JAVA中的配置文件,其后缀名是.properties.它主要用来存放一些全局易修改的的常量配置。
一,properties格式.
properties的格式是key=value,一行一个键值对,不用分号隔开。因为分号是具体的值。
比如:
username=root //它获取的就是"username"的值是"root"
username=root;//它获取的就是"username"的值是"root;"
properties的注释是"#“在行的开头添加一个”#“就能注释当前行的配置,获取添加”!“也行
比如:
#username=root
!username=root
但一般不用”!"来注释
使用properties要知道如何获取一个properties以及里面的键值对数据。以及如何往里面写键值对.
二,properties配置文件获取并取得其中的配置信息
以下是数据库配置文件db.properties的配置信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
user=root
pwd=root
properties常用API
方法 | 返回值 | 描述 |
---|---|---|
load(InputStream(Reader) in) | void | 从输入流中读取键值对,即加载properties.先获取properties的输入流对象。然后再用properties对象的load方法加载它 |
getProperty(String key) | String | 从获取的properties对象中根据key来获取对应配置的值,如果没有该key返回一个null |
setProperty(String key,String value) | void | 从获取的properties对象中写入一个key=value,如果有相同的key则覆盖. |
propertyNames() | Enumeration<?> | 返回所有键的枚举 |
store(Writer(OutputString) writer, String comments) | void | 将properties对象写入到输出流中。使用了setProperty(key,value)后并没有马上就把新的键值对写入到properties中.还需要调用store()方法并创建目标properties的输出流然后传入到store方法中。这也才能把新设置的键值对写入到properties中,comments参数是注释说明可以传null |
1,获取db.properties
获取properties方法有很多种。但常用的一般就是用输入流或者类加载器来获取
public class UtilsJDBC {
public static void propertiesFun(String userName, String pwdr) throws ClassNotFoundException, SQLException {
/**
* 1,先获取目标properties的输入流对象
* 然后再用properties的load方法把键值对加载到properties对象中
* 其中输入流对象的路径,可以是绝对路径可以是相对路径
* 至于相对路径就是当前根目录下的properties.
*/
Reader reader = new FileReader("src/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
/**
* 2,使用当前类的class对象的classLoder来加载properties.
* 然后再用properties的load方法把键值对加载到properties对象中
* 获取ClassPath下的properties.至于路径要怎么写取决于你项目classpath的设置了
* 默认JAVA项目在classes下,也就是src下的文件,src下的properties就直接写.在文件夹下就
* 用"/"隔开,比如test下的db.properties就写成test/properties
* WEB项目在:WEB-INF/Classes
*/
InputStream systemResourceAsStream = ClassLoader.getSystemResourceAsStream("db.properties");
InputStream resourceAsStream = UtilsJDBC.class.getClassLoader().getResourceAsStream("test/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
}
}
2,获取键值对和写入键值对到properties中
public class UtilsJDBC {
public static void propertiesFun(String userName, String pwdr) throws ClassNotFoundException, SQLException {
//加载properties
InputStream resourceAsStream = UtilsJDBC.class.getClassLoader().getResourceAsStream("test/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
/**
* 1,根据key获取值
* 获取到这些数据库连接必要的信息后就可以进行JDBC操作了
*/
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
/**
* 2,获取所有键值对
*/
Enumeration<?> enumeration = properties.propertyNames();
while (enumeration.hasMoreElements()){
Object key = enumeration.nextElement();
String property = properties.getProperty(o.toString());
System.out.println(key+"="+property);
}
/**
* 3,往目标properties写入键值对。
*/
properties.setProperty("name","123456");
Writer writer =new FileWriter("src/db.properties");
//调用setProperty设置了键值对到properties中,在调用store()方法并传入输出流把properties的键值对写
//到目标properties文件中
properties.store(writer,null);
}
}
在上述用getProperty根据key获取value中。就可以获取保存在db.properties的driver,user,name,url。这些数据库连接信息。接着在进行JDBC操作就可以连接数据库了。用配置文件来保存数据库信息能让我们以后要修改数据库连接信息时只要修改配置文件就可以了
总结:
1,properties配置文件主要用来配置全局易修改的常量
2,properties的获取。通过类加载器或者直接创建包含目标properties的输入流。在调用properties对象的load方法来加载properties
3,properties的数据操作。通过基本的getProperties(String key)来根据key获取值。通过setProperties
和包含该properties的输出流以及store方法来把键值对写入到properties