Post

Properties 파일 읽기

1. Properties 파일

1
2
3
4
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.jdbc-url=jdbc:postgresql:/localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

2. Key를 이용하여 Value 가져오기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.Properties;

public class PropertiesValue {

    public static void main(String[] args) {

        String keyName = "spring.datasource.driver-class-name";

        PropertiesValue getPropVal = new PropertiesValue();
        System.out.println(getPropVal.getPropVal(keyName));
    }

    public String getPropVal(String keyName) {

        String value = "";

        try {
            Properties prop = new Properties();
            FileInputStream fis = new FileInputStream("./config/setting.properties");
            prop.load(new BufferedInputStream(fis));
            value = prop.getProperty(keyName).trim();
            fis.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }
}
This post is licensed under CC BY 4.0 by the author.