프로젝트 프로필 xml,ini,porpertys 솔 루 션

프로젝트 자바 가 쓴 데스크 톱 프로그램 은 프로젝트 프로필 을 사용 해 야 하기 때문에 인터넷 자 료 를 한참 동안 찾 아 apache 에 있 는 것 을 사용 하기 로 결 정 했 습 니 다. Commons Configuration 으로 하 겠 습 니 다.기능 이 강 한 것 같 고 다양한 형식 을 호 환 한다 고 합 니 다.
Configuration 은 다음 과 같은 유형의 파일 을 조작 할 수 있 습 니 다.Properties files XML documens Windows INI files Properties list files(plist)JNDI JDBC Datasource System properties Applet parameters Servlet parameters
 
 좋아,지금부터 하 자.
우선 apache 의 comons 안에 있 는 세 개의 가방 을 사용 해 야 합 니 다.
각각 commons-collections-3.2.1,commons-lang-2.4,commons-configuration-1.5.jar
apache 공식 에 가서 이 세 개의 가방 을 다운로드 하 십시오.앞의 두 개가 없 으 면 이상 한 오류 가 발생 할 수 있 습 니 다.
 
 
사용 이 아주 간단 합 니 다.
 
우선 루트 디 렉 터 리 에 xml 프로필 을 만 듭 니 다.
 
<?xml version="1.0" encoding="UTF-8"?>

<!--
    Document   : XMLtest.xml.xml
    Created on : 2008 9 15 ,   9:08
    Author     : Administrator
    Description:
        Purpose of the document follows.
-->

<gui-definition>  
    <colors>  
        <background>#808080</background>  
        <text>#000000</text>  
        <header>#008000</header>  
        <link normal="#000080" visited="#800080"/>  
        <default>${colors.header}</default>  
    </colors>  
    <rowsPerPage>15</rowsPerPage>  
    <buttons>  
        <name>OK,Cancel,Help</name>  
        <name>Yes,No,Cancel</name>  
    </buttons>  
    <numberFormat pattern="###\,###.##"/>  
</gui-definition>
 
 
 
우선 자원 파일 로드
 
XMLConfiguration config = new XMLConfiguration();
            config.setDelimiterParsingDisabled(true);
            config.setFileName("XMLtest.xml");
            config.load();
 
 
읽 기 진행
 
List<String> buttons = config.getList("buttons.name");
            for (String button : buttons) {
                System.out.println(button);
            }
 
 
사실 사용 은 config.get 데이터 형식 입 니 다.예 를 들 어 config.getString,config.getDouble 등 편리 함 을 얻 을 수 있 습 니 다.
 
설정 파일 의 내용 이 바 뀌 었 을 때 Commons Configuration 은 설정 파일 의 내용 을 자동 으로 다시 읽 을 수 있 는 특수 한 기능 도 있 습 니 다.
 
사용 방법
XMLConfiguration config = new XMLConfiguration(filename);
      config.setReloadingStrategy(new FileChangedReloadingStrategy());
 
 
 

좋은 웹페이지 즐겨찾기