新建一個普通的Java項目,在src目錄下有一config.propertes文件。配置文件裡面的內容如下(就一句話):
driverURL = jdbc:mysql://127.0.0.1:3306/evan
下面是讀取配置文件的java代碼:
package com.evan;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadProperties {
public static void main(String[] args) throws IOException {
InputStream is = ReadProperties.class.getResourceAsStream("/config.properties");
Properties properties = new Properties();
properties.load(is);
is.close();
String driverURL = properties.getProperty("driverURL");
/*
* 輸入結果為:jdbc:mysql://127.0.0.1:3306/evan
*/
System.out.println(driverURL);
}
}
項目的目錄如下: