java/코드 리뷰

14. etc - (10) PropertiesDemo (파일 설정 프로그램)

Astaroth아스 2020. 4. 24. 13:55

config.properties

 

1
2
3
4
5
host.url=192.168.10.2
host.port=3000
host.username=admin
host.password=zxcv1234
 

 

1
2
3
4
5
6
7
#Mail Server Configuration
#Wed Apr 08 15:43:42 KST 2020
mail.server.url=192.168.10.2
mail.server.username=admin
mail.server.port=3000
mail.server.password=zxc1234
 

mail.properties

 

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
package etc;
 
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
 
public class PropertiesDemo {
    
    public static void main(String[] args) throws IOException {
        
        Properties prop = new Properties();
        
        prop.load(new FileReader("src/config.properties"));
//        System.out.println(prop);
        
        String url = prop.getProperty("host.url");
        String port = prop.getProperty("host.port");
        String username = prop.getProperty("host.username");
        String password = prop.getProperty("host.password");
        
        System.out.println("URL : " + url);
        System.out.println("PORT번호 : " + port);
        System.out.println("아이디 : " + username);
        System.out.println("비밀번호 : " + password);
        
    }
}
 
 
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
package etc;
 
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
 
public class PropertiesDemo2 {
    
    public static void main(String[] args) throws IOException {
        
        Scanner sc = new Scanner(System.in);
        System.out.println("파일 설정 프로그램");
        System.out.print("서버 URL을 입력하세요 : ");
        String url = sc.next();
        System.out.print("서버의 포트 번호를 입력하세요 : ");
        String port = sc.next();
        System.out.print("서버 접속 계정을 입력하세요 : ");
        String username = sc.next();
        System.out.print("서버 비밀번호를 입력하세요 : ");
        String password = sc.next();
        
        Properties prop = new Properties();
        prop.setProperty("mail.server.url", url);
        prop.setProperty("mail.server.port", port);
        prop.setProperty("mail.server.username", username);
        prop.setProperty("mail.server.password", password);
        
        prop.store(new FileWriter("src/mail.properties"), "Mail Server Configuration");
        
        sc.close();
        
    }
}