Java读取properties配置文件

Posted on 2009-06-17 22:38 eric_xu 阅读(246) 评论(0)  编辑  收藏 所属分类: Java

package sjtu.rfid;

import java.util.ResourceBundle;

public class ConfigBundle {
    
private static ResourceBundle configBundle = null;
    
    
static{
        configBundle 
= ResourceBundle.getBundle("config");
    }
    
    
public static String getUsername() {
        String username 
= null;

        
try {
            username 
= configBundle.getString("username");
        } 
catch (Exception e) {
            
return null;
        }

        
return username;
    }
    
    
public static String getPassword() {
        String password 
= null;

        
try {
            password 
= configBundle.getString("password");
        } 
catch (Exception e) {
            
return null;
        }

        
return password;
    }
    
    
public static void main(String[] args){
        
        System.out.println(ConfigBundle.getUsername());
        System.out.println(ConfigBundle.getPassword());
        
    }
}

将XML转为Properties处理
        Properties prop = new Properties();
        prop.loadFromXML(ClassLoader.getSystemResourceAsStream(CONFIG));
        String url 
= prop.getProperty(URL);
        String user 
= prop.getProperty(USER);
        String password 
= prop.getProperty(PASSWORD);


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 37, comments - 5, trackbacks - 0, articles - 0

Copyright © eric_xu