๋ฐ์ํ
- application.properties
people.name="iseunghan"
people.age=26
people.city="Daejeon"
- Main.java
public class Main {
static String name;
static String age;
static String city;
private static void read_properties() {
// resource ์์น
String resource = "META-INF/application.properties";
Properties properties = new Properties();
try {
InputStream inputStream = Main.class.getClassLoader().getResourceAsStream(resource);
if (inputStream != null) {
properties.load(inputStream);
} else {
throw new FileNotFoundException("file not found");
}
} catch (IOException e) {
e.printStackTrace();
}
// ๋ฏธ๋ฆฌ ์ ์ธํด๋ String ๋ณ์๋ค
name = properties.getProperty("people.name");
age = properties.getProperty("people.age");
city = properties.getProperty("people.city");
}
public static void main(String[] args) {
read_properties();
System.out.println("name = " + name);
System.out.println("age = " + age);
System.out.println("city = " + city);
}
}
properties.getProperty()
๋ ํญ์ String์ผ๋ก ๋ฆฌํดํฉ๋๋ค.
age๋ฅผ int๋ก ์ ์ธํ๋๋ ๋ฐ์ํ ์ค๋ฅ
- ์คํ ๊ฒฐ๊ณผ
์ ์์ ์ผ๋ก properties
๊ฐ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
๋ฐ์ํ