1. 程式人生 > >Java在類中引入properties

Java在類中引入properties

將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