2012-08-08 5 views
1

java.util.prefs.Preferences을 사용하여 내 응용 프로그램에 대한 참조를 만들었지 만 xml 파일에서 환경 설정을로드 할 수없는 것으로 보입니다. 나는 파일을 찾을 수 없으므로 파일을로드하고있다. 단지 파일을 파싱하지 않는 것 같습니다. 누군가 내가 내가 뭘 잘못하고 있는지 알려 줄 수 있니?환경 설정 라이브러리가 환경 설정을로드하지 않음

명확화 예 파일을로드하는 중입니다. 'root.exportSubtree (System.out)'행은 전체 XML을 출력합니다. 그러나 내가 취한 예에서는 루트 '사용자'의 서브 루트 만 표시했습니다. 그게 내 문제의 힌트 일지 모르지만 나는 아직 해결하지 못했다.

내 환경 설정 클래스

package com.g4apps.secure.processserver; 

import java.io.BufferedInputStream; 
import java.io.FileInputStream; 
import java.io.InputStream; 
import java.util.prefs.Preferences; 

public class PSPrefs { 
     private String user; 
     private String url; 
     private String password; 
     private String database; 

     public PSPrefs(String file) { 
      InputStream is = null; 

      try { 
       is = new BufferedInputStream(new FileInputStream(file)); 
       Preferences.importPreferences(is); 
       Preferences root = Preferences.userRoot(); 
       root.exportSubtree(System.out); 
       this.user=root.get("user", ""); 
       this.url=root.get("url", ""); 
       this.password=root.get("password",""); 
       this.database=root.get("database",""); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     public String getUser() { 
      return this.user; 
     } 
     public String getURL() { 
      return this.url; 
     } 
     public String getPassword() { 
      return this.password; 
     } 
     public String getDatabase() { 
      return this.database; 
     } 


} 

내 preferences.xml로 파일.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> 
<preferences EXTERNAL_XML_VERSION="1.0"> 
    <root type="user"> 
     <map/> 
     <node name="Server Settings"> 
      <map> 
       <entry key="user" value="user"/> 
       <entry key="url" value="jdbc:mysql://mysqldb.com/"/> 
       <entry key="password" value="password"/> 
       <entry key="database" value="mydb"/> 
      </map> 
     </node> 
    </root> 
</preferences> 

주셔서 감사합니다.

답변

0

글쎄, 나에게 나오는 것은 그 파일이 당신이 생각하는 곳이 아니라는 것입니다. 파일이 실행 컨텍스트 내에서 올바른 위치에 있는지 확인 file.exists()

가진 파일의 존재를 확인하십시오

UPDATE

좋아, 미안 나의 나쁜 :(

이 시도 :

try { 
    is = new BufferedInputStream(new FileInputStream(file)); 
    Preferences.importPreferences(is); 
    Preferences root = Preferences.userRoot(); 

    Preferences node = Preferences.userRoot().node("Server Settings"); 

    this.user = node.get("user", ""); 
    this.url = node.get("url", ""); 
    this.password = node.get("password", ""); 
    this.database = node.get("database", ""); 

    System.out.println(user); 
    System.out.println(url); 
    System.out.println(password); 
    System.out.println(database); 

} catch (Exception e) { 
    e.printStackTrace(); 
} 

이렇게하면 출력이

이됩니다.
user 
jdbc:mysql://mysqldb.com/ 
password 
mydb 

오, 당신이 roseindia에서 예를 사용한다면, 실제로 전체 환경 설정 트리를 버려진, 그들은 단지 "강조"

+0

그래 그게 거기에 지점이 아니다. 나는 xml 파일을 잘 읽고있다. root.exportSubTree (System.out) 행은 전체 XML을 표시하지만. 예제에서 나는 그것을 취했을 때 type = user 아래에 서브 트리 만 표시했습니다. – Codeguy007

+0

@ Codeguy007 죄송합니다. 아침에 카페인 잔을 마셔야합니다 : P – MadProgrammer

+0

그래, 그게 전부 생산인지 아닌지는 확실치 않습니다. 외모가 자른다. 가치를 얻는 방법을 알려주기 전에 멈추는 것이 가장 좋은 예는 아니지만 찾을 수있는 최선의 방법이었습니다. – Codeguy007