如果配置文件是XML,我又如何做呢?

解决方案 »

  1.   

    HashTable ht=new HashTable();
    ht.put("mainlog_dir",new String("D:/ProgramFiles/eclipse3.1/workspace/JasonLong/src/power/log/"));
    ....如果是xml文件用jndi来读一般配置信息是用.perperty文件存放,然后用读取Property信息
      

  2.   

    哦,用.perperty文件存放,我又如何读取呢,能不能举个例子呢?
      

  3.   

    Properties props = new Properties();
    props.load(this.getClassLoader().getClass().getResourceAsStream("/config/config.properties"));
    String mainlog_dir="";
    mainlog_dir=props.getProperty("mainlog_dir");/config/config.properties这个文件里面应该有下面的内容
    mainlog_dir=D:/Program Files/eclipse3.1/workspace/JasonLong/src/power/log/
    forname=org.postgresql.Driver
    dbsource=jdbc:postgresql://192.168.1.10:5432/powerdb
    username=postgre
    passwd=postgre
    findfie_interval=5
    data_dir=D:、data/power/
    backup_dir=D:、data/
      

  4.   

    楼上的,非常谢谢,但我按照你写的例子做,但不行,props.load(this.getClassLoader().getClass().getResourceAsStream,这行报错,代码如下:
    public class test {    public static void main(String[] args) {
            Properties props = new Properties();
            props.load(this.getClassLoader().getClass().getResourceAsStream("/power/config.properties"));
            String mainlog_dir="";
            mainlog_dir=props.getProperty("mainlog_dir");        System.out.println("mainlog_dir=["+mainlog_dir+"]");
            
        }
    }
      

  5.   

    props.load(new FileInputStream(new File("/power/config.properties")));
      

  6.   

    还是报错,错误如下:
    java.io.FileNotFoundException: D:\Program Files\eclipse3.1\workspace\JasonLong\src\test\test\config.properties (系统找不到指定的路径。)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at test.test.main(test.java:27)
      

  7.   

    那你是的路径问题啊
    你要确定/power/config.properties是对的
      

  8.   

    楼主的错误我知道是怎么回事,不同的IDE的当前路径可能有所不同,所以可以运行的,你就可能会出错,所以,你要按错误提示,把config.properties放在:D:\Program Files\eclipse3.1\workspace\JasonLong\src\test\test\config.properties
      

  9.   

    public class PropertiesConfig{
    public static final String module = PropertiesConfig.class.getName();
    public static final Properties prop = new Properties();
    private static PropertiesConfig propertiesConfig= null;
    private PropertiesConfig(){
      
      
     };
    synchronized public PropertiesConfig getPropInstance(){
    if(propertiesConfig==null){
         propertiesConfig = new PropertiesConfig() ;
    }
    return propertiesConfig;
    }
    public static void loadProperties(){
       try{
         File f = new File(".");         
             String path = f.getAbsolutePath();
             path = path.substring(0,path.length()-1)+"config.Properties";
             //System.out.println(path);
         InputStream inputStream = new FileInputStream(path);
         prop.load(inputStream);
       }catch(FileNotFoundException e){    
         System.out.println("[File NOT FOUND Exception]"+e.toString());
       }catch(IOException e){
         System.out.println(e.toString());
       }


    }
    public static Properties getProperties(){
    loadProperties();
    return prop;
    }
    public static void main(String[] args){
    //Debug.logInfo(module);
    System.out.println(module);

    loadProperties();
    System.out.println((String)prop.get("CONTROLLER_XML"));

    }

    }
      

  10.   

    确定路径没问题,包名加全没有啊,还有你的config.properties 有没有builder到class里面啊