'Property'에 해당되는 글 3건

  1. 2016.03.03 [Java] 외부 프라퍼티(XML) 파일 설정 정보 읽기
  2. 2013.04.05 [java] read properties or xml file.
  3. 2012.05.22 [C언어] 설정파일 읽기

[Java] 외부 프라퍼티(XML) 파일 설정 정보 읽기

ITWeb/개발일반 2016.03.03 16:30

외부에 존재하는 properties 파일이나 xml 파일에서 설정 정보를 읽고 싶을 경우가 있습니다.

그래서 기록해 봅니다.


import java.io.*;

import java.util.Properties;


Properties properties = new Properties();

InputStreamReader isr = null;


try {

  isr = new InputStreamReader(new FileInputStream("/server/config/config.properties"), "UTF-8");

  properties.load(isr);

} finally {

  if (null != isr) {

    try {

      isr.close();

    } catch (IOException ex) {}

  }

}


System.out.println(properties.getProperty("프라퍼티명"));


xml을 읽고 싶을 경우 

properties.loadFromXML(InputStream in);

을 이용하시면 됩니다.


저작자 표시 비영리 변경 금지
신고
크리에이티브 커먼즈 라이선스
Creative Commons License
Trackback 0 : Comment 0

[java] read properties or xml file.

ITWeb/개발일반 2013.04.05 10:57

http://javarevisited.blogspot.kr/2012/03/how-to-read-properties-file-in-java-xml.html


[properties file]

jdbc.username=test

jdbc.password=unknown

jdbc.driver=com.mysql.jdbc.Driver


[source code]

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;


public class PropertyFileReader {


    public static void main(String args[]) throws FileNotFoundException, IOException {


        //Reading properties file in Java example

        Properties props = new Properties();

        FileInputStream fis = new FileInputStream("c:/jdbc.properties");

      

        //loading properites from properties file

        props.load(fis);


        //reading proeprty

        String username = props.getProperty("jdbc.username");

        String driver = props.getProperty("jdbc.driver");

        System.out.println("jdbc.username: " + username);

        System.out.println("jdbc.driver: " + driver);


    }

}


[xml file]

<?xml version="1.0" encoding="UTF-8" standalone="no"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

   <properties> <entry key="jdbc.username">root</entry>

        <entry key="jdbc.password">mysql</entry>

   </properties>


[source code]

public static void main(String args[]) throws FileNotFoundException, IOException {


        //Reading properties file in Java example

        Properties props = new Properties();

        FileInputStream fis = new FileInputStream("c:/properties.xml");

      

        //loading properites from properties file

        props.loadFromXML(fis);


        //reading proeprty

        String username = props.getProperty("jdbc.username");

        System.out.println("jdbc.username: " + username);

      

}


신고
크리에이티브 커먼즈 라이선스
Creative Commons License
tags : Property, XML
Trackback 0 : Comment 0

[C언어] 설정파일 읽기

ITWeb/개발일반 2012.05.22 23:17

[Build & Run]

gcc -c config.c; gcc config.o -o config

./config /home/jjeong/workspace/config/config.property

[config.property]

# UserAuthUrl
UserAuthUrl^http://192.168.1.2:8080/rcpt_auth?email=%s
# UserAuthCompareStr
UserAuthCompareStr^verified
# DlAuthUrl
DlAuthUrl^http://192.168.1.2:8080/dlCheck?q={filterSearch:'%s'}
# DlAuthCompareStr
DlAuthCompareStr^SUCCESS
# AhfServer
AhfServer^192.168.1.2
# AhfPort
AhfPort^33000
#AhfUser
AhfUser^jjeong
#AhfPass
AhfPass^1234
#AhfDb
AhfDb^demodb
#AhfQuery
AhfQuery^SELECT * FROM event WHERE id='%s'
#SkipDomain
SkipDomain^jjeong.com

[config.c]

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct config {
        char userAuthUrl[256];
        char userAuthCompareStr[128];
        char dlAuthUrl[256];
        char dlAuthCompareStr[128];
        char ahfServer[128];
        int ahfPort;
        char ahfUser[128];
        char ahfPass[128];
        char ahfDb[128];
        char ahfQuery[128];
        char skipDomain[128];
} CONFIG;

int isBlank(char ch) {
    return ( (ch==0x20) || (ch=='\t') || (ch=='\r') || (ch=='\n') );
}

char *trim( char *s ) {
    char *f=s, *e=0, *c=s;

    /* 뒤쪽 공백 제거 */
    e=s +(strlen(s)) -1;
    while( isBlank(*e) && s<=e) e--;
    *(e+1)=0;

    /* 앞쪽 공백 제거 */
    while( isBlank(*f) && f<=e) f++;

    /* 공백 없는 부분 복사 */
    if (s!=f) {
        while(f<=e) *(c++) = *(f++);
        *c=0;
    }

    return s;
}

int setConfig(CONFIG *conf, char *confFile) {
        char readBuff[512];
        char varBuff[256];
        char *temp;

        FILE *fp;

        memset(varBuff, 0, 256);

        fp = fopen(confFile, "r");

        if ( fp ){
                while ( !feof(fp) ) {
                        memset(readBuff, 0, 512);

                        if ( fgets(readBuff, 512, fp) == NULL ) {
                                continue;
                        }

                        temp = trim(readBuff);

                        if ( *temp == '#' ) {
                                continue;
                        }

                        if ( strncasecmp(temp, "UserAuthUrl", strlen("UserAuthUrl")) == 0 ) {
                                strcpy(conf->userAuthUrl, strrchr(temp, '^')+1);
                                snprintf(varBuff, sizeof(varBuff), conf->userAuthUrl, "sadfasdfsafasfsdafsafsadf");
                                printf("UserAuthUrl : %s\n", varBuff);
                                memset(varBuff, 0, 256);
                        }

                        if ( strncasecmp(temp, "UserAuthCompareStr", strlen("UserAuthCompareStr")) == 0 ) {
                                strcpy(conf->userAuthCompareStr, strrchr(temp, '^')+1);
                                printf("UserAuthCompareStr : %s\n", conf->userAuthCompareStr);
                        }

                        if ( strncasecmp(temp, "DlAuthUrl", strlen("DlAuthUrl")) == 0 ) {
                                strcpy(conf->dlAuthUrl, strrchr(temp, '^')+1);
                                snprintf(varBuff, sizeof(varBuff), conf->dlAuthUrl, "mail@jjeong.com");
                                printf("DlAuthUrl : %s\n", varBuff);
                                memset(varBuff, 0, 256);

                        }

                        if ( strncasecmp(temp, "DlAuthCompareStr", strlen("DlAuthCompareStr")) == 0 ) {
                                strcpy(conf->dlAuthCompareStr, strrchr(temp, '^')+1);

                                if ( strstr("SUCCESS", conf->dlAuthCompareStr) ) {
                                        printf("DlAuthCompareStr SUCCESS\n");
                                }
                                printf("DlAuthCompareStr : %s\n", conf->dlAuthCompareStr);
                        }

                        if ( strncasecmp(temp, "AhfServer", strlen("AhfServer")) == 0 ) {
                                strcpy(conf->ahfServer, strrchr(temp, '^')+1);
                                printf("AhfServer : %s\n", conf->ahfServer);
                        }

                        if ( strncasecmp(temp, "AhfPort", strlen("AhfPort")) == 0 ) {
                                conf->ahfPort = atoi(strrchr(temp, '^')+1);
                                printf("AhfPort : %d\n", conf->ahfPort);
                        }

                        if ( strncasecmp(temp, "AhfUser", strlen("AhfUser")) == 0 ) {
                                strcpy(conf->ahfUser, strrchr(temp, '^')+1);
                                printf("AhfUser : %s\n", conf->ahfUser);
                        }

                        if ( strncasecmp(temp, "AhfPass", strlen("AhfPass")) == 0 ) {
                                strcpy(conf->ahfPass, strrchr(temp, '^')+1);
                                printf("AhfPass : %s\n", conf->ahfPass);
                        }

                        if ( strncasecmp(temp, "AhfDb", strlen("AhfDb")) == 0 ) {
                                strcpy(conf->ahfDb, strrchr(temp, '^')+1);
                                printf("AhfDb : %s\n", conf->ahfDb);
                        }

                        if ( strncasecmp(temp, "AhfQuery", strlen("AhfQuery")) == 0 ) {
                                strcpy(conf->ahfQuery, strrchr(temp, '^')+1);
                                snprintf(varBuff, sizeof(varBuff), conf->ahfQuery, "TEST");
                                printf("AhfQuery: %s\n", varBuff);
                                memset(varBuff, 0, 256);
                        }

                        if ( strncasecmp(temp, "SkipDomain", strlen("SkipDomain")) == 0 ) {
                                strcpy(conf->skipDomain, strrchr(temp, '^')+1);
                                printf("SkpDomain : %s\n", conf->skipDomain);
                        }

                }
        } else {
        }

        fclose(fp);

        return 1;
}


int main(int argc, char *argv[]) {
        CONFIG conf;

        printf("file full path : %s\n", argv[1]);

        setConfig(&conf, argv[1]);

        return 1;
}

일부 코드는 레퍼런스 코드 가져다 사용하였습니다.

뭐 이런 종류의 코드는 구글링 하면 많이 나오니까.. ^^;;

신고
크리에이티브 커먼즈 라이선스
Creative Commons License
tags : c, config, Property
Trackback 0 : Comment 0

티스토리 툴바