1、文件目录
2、配置文件内容
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://mydb:3306/test?characterEncoding=utf8&useSSL=true&serverTimezone=UTC
db.username=root
db.password=toor
3、读取方法
package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class Test {
public static void main(String[] args) throws IOException {
File file = new File("lib/db.properties");
if(!file.exists() && file.isDirectory()){
throw new FileNotFoundException("配置文缺少件");
}
Properties prop = new Properties();
FileInputStream in = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
prop.load(reader);
String url = prop.getProperty("db.url");
System.out.println("url: "+url);
}
}
4、结果