properties配置文件内容如下:
driverName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_accountSystem
user=root
password=admin
Java代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws IOException {
// 实例化Properties对象
Properties properties = new Properties();
// 加载properties配置文件
properties.load(new FileInputStream(new File("src\\AccountSystem\\dao\\db.properties")));
// 通过键名获取对应的值
String driverName = properties.get("driverName").toString();
String url = properties.get("url").toString();
String user = properties.get("user").toString();
String password = properties.get("password").toString();
// 控制台打印
System.out.println("driverName: " + driverName + "\nurl: " + url + "\nuser: " + user + "\npassword: " + password);
}
}
控制台打印: