加載配置文件

前言:若是咱們須要調用某個方法,其中的參數爲可更改,咱們最好是採用配置文件的方式來寫,這樣便於管理java

好比我在寫一個經過SFTP鏈接服務器的環節apache

public class SFTPUtil {
    
    private static Logger log=Logger.getLogger(SFTPUtil.class.getName());
    
    private String host;//服務器鏈接ip
    private String username;//用戶名
    private String password;//密碼
    private int port=22;//端口號
    private ChannelSftp sftp=null;
    private Session sshSession=null;
    
    
    public SFTPUtil(String host, String username, String password, int port) {
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
    }


    public SFTPUtil(String host, String username, String password) {
        super();
        this.host = host;
        this.username = username;
        this.password = password;
    }
    
    /**
     *ͨ經過SFTP鏈接服務器
     */
    public void connect(){
        try {
            JSch jsch=new JSch();
            jsch.getSession(username, host, port);
            //����session
            sshSession=jsch.getSession(username, host, port);
            if(log.isInfoEnabled()){
                log.info("Session created.");
            }
            sshSession.setPassword(password);
            Properties sshConfig=new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            if(log.isInfoEnabled()){
                log.info("Session connected.");
            }
            Channel channel=sshSession.openChannel("sftp");
            channel.connect();
            if(log.isInfoEnabled()){
                log.info("Opening channel.");
            }
            sftp=(ChannelSftp)channel;
            if(log.isInfoEnabled()){
                log.info("Connected to "+host+".");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
View Code

1.Properties文件

以key-value的形式寫入參數服務器

 

2.加載properties的工具類

package com.copote.commons.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConfigurableConstants {
    
    public static Log logger = LogFactory.getLog(ConfigurableConstants.class);
    public static Properties p = new Properties();

        /**
         * 靜態讀入屬性文件到Properties p變量中
         */
        public  static  void init() {
            InputStream in = null;
            try {
                in = ConfigurableConstants.class.getClassLoader().getResourceAsStream("config.properties");             
                if (in != null)
                    p.load(in);            
            } catch (IOException e) {
                logger.error("load config.properties into Constants error!");
            }
            finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        logger.error("close config.properties error!");
                    }
                }
            }
        }

        /**
         * 封裝了Properties類的getProperty函數,使p變量對子類透明.
         *
         * @param key          property key.
         * @param defaultValue 當使用property key在properties中取不到值時的默認值.
         */
        public static String getProperty(String key) {
            return p.getProperty(key);
        }
        
        public static void main(String[] args){
            ConfigurableConstants.init();
            System.out.println(ConfigurableConstants.getProperty("host"));
        }
}
View Code

3.調用

相關文章
相關標籤/搜索