使用步骤:
创建properties文件
配置properties文件
读取外部文件内容
创建properties文件
1.打开IDEA
2.选中项目后,右键点击"new",点击"File"
3.文件后缀名需要为"properities"
配置properties文件
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/Ebook_UI
user=root
password=1234
这里将对应的信息进行填充即可
读取外部文件内容
使用Properties集合和字节流读取外部文件内容
public static void main(String[] args) {
//创建对象读取外部配置的内容
Properties prop = new Properties();
try {
FileInputStream fis = new FileInputStream(“jdbc.properties”);
//加载外部配置文件的内容
prop.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
//读取外部配置文件的内容
String driverclass = prop.getProperty(“driverclass”);
String ur1 = prop.getProperty(“ur1”);
String user = prop.getProperty(“user”);
String password = prop.getProperty(“password”);
System.out.println(driverclass);
}