Java在類中引入properties
阿新 • • 發佈:2019-01-25
將properties檔案放入src下、通過java程式碼讀取
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class test3 {
public static void main(String[] args) throws IOException {
InputStream is = test3.class.getClassLoader().getResourceAsStream(“jdbc.properties”);
Properties p = new Properties(); p.load(is); String username = p.getProperty("jdbc.username"); String password = p.getProperty("jdbc.password"); String localhost = p.getProperty("jdbc.host"); String port = p.getProperty("jdbc.port"); System.out.println(username); System.out.println(password); System.out.println(localhost); System.out.println(port); }
}
properties檔案如下:
jdbc.username=root
jdbc.password=root
jdbc.host=localhost
jdbc.port=3306