浏览代码

Properties 클래스 수정

tobby48 6 年前
父节点
当前提交
1fb8d83dfa
共有 1 个文件被更改,包括 50 次插入82 次删除
  1. 50
    82
      src/kr/co/swh/lecture/java/example/PropertiesDemo.java

+ 50
- 82
src/kr/co/swh/lecture/java/example/PropertiesDemo.java 查看文件

@@ -17,86 +17,54 @@ import java.util.Properties;
17 17
  * @version : v1.0
18 18
  */
19 19
 public class PropertiesDemo {
20
-	private Properties properties;
21
-
22
-    public PropertiesDemo() {
23
-        properties = new Properties();
24
-    }
25
-
26
-    public Properties getProperties() {
27
-        return properties;
28
-    }
29
-
30
-    /**
31
-    * 이 메소드는 프로퍼티 파일을 스트림으로 읽어 들여 멤버 변수의 프로퍼티에 적재합니다.
32
-    *
33
-    * @param path
34
-    * @throws IOException
35
-    */
36
-    public void loadProp(String path) throws IOException {
37
-        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
38
-        properties.load(inputStream);
39
-        inputStream.close();
40
-       }
41
-
42
-    /**
43
-    * 이 메소드는 static으로 선언해서 프로퍼티 파일을 읽는 것을 보여줍니다.
44
-    * @param path
45
-    * @return
46
-    * @throws IOException
47
-    */
48
-    public static Properties loadPropForStatic(String path) throws IOException {
49
-        Properties properties = new Properties();
50
-        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
51
-        properties.load(inputStream);
52
-        inputStream.close();
53
-        return properties;
54
-    }
55
-
56
-    public static void main(String[] args) throws IOException {
57
-        PropertiesDemo propertiesDemo = new PropertiesDemo();
58
-
59
-        // 프로퍼티 파일을 읽어들이고 해당 값을 출력해봅니다.
60
-        propertiesDemo.loadProp("application.properties");
61
-        Properties properties = propertiesDemo.getProperties();
62
-        properties.list(System.out);
63
-
64
-        // 아래 코드는 새로운 프로퍼티 파일에 같은 키를 준 경우 오버라이드 하는 것을 확인합니다.
65
-        Properties staticProp = PropertiesDemo.loadPropForStatic("application-prod.properties");
66
-        properties.putAll(staticProp);
67
-        properties = propertiesDemo.getProperties();
68
-        properties.list(System.out);
69
-
70
-        // 아래 코드는 프로퍼티간의 결합을 확인합니다.
71
-        Properties dummy = new Properties();
72
-        dummy.put("demo.type", "dummy"); // 기존 키를 오버라이드 합니다.
73
-        dummy.put("demo.temp", "temp"); // 새로운 키를 추가합니다.
74
-        properties.putAll(dummy); // 기존 프로퍼티에 더미 프로퍼티를 결합합니다.
75
-        properties.list(System.out);
76
-
77
-        // 아래 코드는 개별 키를 주어 값을 반환받습니다.
78
-        Object type = properties.get("demo.type");
79
-
80
-        // 아래코드는 프로퍼티를 키들을 순회하는 구문입니다.
81
-        // .stringPropertyNames 대신 .keySet 을 사용할수도 있습니다.
82
-        for (String key : properties.stringPropertyNames()) {
83
-            Object value = properties.getProperty(key);
84
-        }
85
-
86
-        // 해당 키가 있는지 여부를 판별합니다.
87
-        properties.containsKey("demo.type");
88
-
89
-        // 해당 값이 있는지 여부를 판별합니다.
90
-        properties.containsValue("dummy");
91
-
92
-        // 키값을 주고 해당 값이 있으면 해당 값을 반환하지만
93
-        // 해당 값이 없으면 null을 반환합니다.
94
-        Object result = properties.computeIfAbsent("undefined", value -> returnNull(value));
95
-        System.out.println("result value is " + result);
96
-    }
97
-
98
-    public static Object returnNull(Object key) {
99
-        System.out.println(key + " value is null.");
100
-        return null;
101
-    }
20
+	/**
21
+	 * 프로퍼티 파일을 스트림으로 읽음
22
+	 *
23
+	 * @param path
24
+	 * @throws IOException
25
+	 */
26
+	public Properties loadProp(String path) throws IOException {
27
+		Properties properties = new Properties();
28
+		InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
29
+		properties.load(inputStream);
30
+		inputStream.close();
31
+		return properties;
32
+	}
33
+
34
+	public static void main(String[] args) throws IOException {
35
+		PropertiesDemo propertiesDemo = new PropertiesDemo();
36
+
37
+		// 프로퍼티 파일을 읽어들이고 해당 값을 출력
38
+		Properties properties = propertiesDemo.loadProp("application.properties");
39
+		properties.list(System.out);
40
+
41
+		// 같은 키를 준 경우 오버라이드 하는 것을 확인
42
+		Properties staticProp = propertiesDemo.loadProp("application-prod.properties");
43
+		properties.putAll(staticProp);
44
+		properties.list(System.out);
45
+
46
+		// 프로퍼티간의 결합
47
+		Properties dummy = new Properties();
48
+		dummy.put("demo.type", "dummy");
49
+		dummy.put("demo.temp", "temp"); 	// 새로운 키를 추가
50
+		properties.putAll(dummy); 			// 기존 프로퍼티에 더미 프로퍼티를 결합
51
+		properties.list(System.out);
52
+
53
+		// 키를 주어 값을 리턴
54
+		Object type = properties.get("demo.type");
55
+		System.out.println(type);
56
+
57
+		// 프로퍼티 키들을 순회
58
+		// stringPropertyNames() 대신 다른 메소드 사용 가능
59
+		for (String key : properties.stringPropertyNames()) {
60
+			Object value = properties.getProperty(key);
61
+			System.out.println(value);
62
+		}
63
+
64
+		// 해당 키가 있는지 여부를 판별합니다.
65
+		System.out.println(properties.containsKey("demo.type"));
66
+
67
+		// 해당 값이 있는지 여부를 판별합니다.
68
+		System.out.println(properties.containsValue("dummy"));
69
+	}
102 70
 }