系统配置文件properties
1. properties文件的介绍
在做Java项目开发过程中,涉及到一些数据库服务连接配置、缓存服务器连接配置等,通常情况下我们会将这些不太变动的配置信息存储在以 .properties 结尾的配置文件中
为了让Java程序可以读取 .properties配置文件中的值,Java的JDK中提供了java.util.Properties类可以实现读取配置文件。
2. properties常用API的介绍
2.1读取系统配置文件的常用api
| public Properties() | 用于构建Properties集合对象 |
|---|
| public void load(InputStresm is) | 通过字节输入流,读取属性文件里面的内容 |
| public void load(Reader reader) | 通过字符输入流,读取属性文件里的键值对数据 |
| public String getProperty(String key) | 根据键获取值(其实就是get方法的效果) |
| public Set stringPropertyNames() | 获取全部键的集合(其实就是ketSet方法的效果) |
2.2文件读取的使用举例
public class PropertiesTest1 {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(new FileReader("E:\\JAVA_Project\\Javase\\src\\users.properties"));
System.out.println(properties);
System.out.println(properties.getProperty("迷麟"));
System.out.println(properties.getProperty("蛮吉"));
Set<String> key = properties.stringPropertyNames();
for (String s : key) {
String value = properties.getProperty(s);
System.out.println(s+"=====>"+value);
}
properties.forEach((k,v)-> System.out.println(k+"--->"+v));
}
}
2.3写入系统配置文件的常用api
| public Properties() | 用于构建Properties集合对象(空容器) |
|---|
| public Object setProperty(String key, String value) | 保存键值对数据到Properties对象中去。 |
| public void store(OutputStream os, String comments) | 把键值对数据,通过字节输出流写出到属性文件里去 |
| public void store(Writer w, String comments) | 把键值对数据,通过字符输出流写出到属性文件里去 |
2.4文件写入的使用举例
public class PropertiesTest2 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.setProperty("星云","雪儿");
properties.setProperty("龙泉","素心");
properties.setProperty("子凡","林轩");
properties.store(new FileWriter("E:\\JAVA_Project\\Javase\\src\\users2.properties"),
"新增的用户对象");
}
}
3. properties在maven一般是配置数据库的连接
3.1 在数据库中的常用配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/d_1xt
spring.datasource.username=root
spring.datasource.password=root
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.configuration.map-underscore-to-camel-case=true
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB
aliyun.oss.endpoint=https://oss-cn-guangzhou.aliyuncs.com
aliyun.oss.accessKeyId=LTA
aliyun.oss.accessKeySecret=228
aliyun.oss.bucketName=java-web-pcs