2018년 11월 1일 목요일

[Java] Properties Load and Save Example



1. Properties Load and Save Example


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

public class Settings {

    Properties settings;
    
    public Settings() {
        settings = new Properties();
        load();
    }
    
    public void load() {    
        InputStream input = null;
        try {
            input = new FileInputStream("config.properties");
            settings.load(input);
        } catch (IOException ex) {
                ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public void save() {
        OutputStream output = null;
        try {
            output = new FileOutputStream("config.properties");
            settings.store(output, null);
        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public void set(String name, String value) {
        settings.setProperty(name, value);
    }
    
    public String get(String name) {
        return settings.getProperty(name);
    }
}

댓글 없음:

댓글 쓰기