1.先在專案中建立一個套件(如:config),再建立一個設定檔(如:a.properties),新增設定資訊如下:
比如:
複製代碼代碼如下:
name=kaka
age=28
2、代碼:
複製代碼代碼如下:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
PropertyTest loadProp = new PropertyTest();
InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties");
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(prop.getProperty("name"));
System.out.println(prop.getProperty("age"));
}
}