使用java.util.Properties工具制作自定义访问配置文件信息

import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * Created by Administrator on 2019/3/25.
 */
public class propertiesutil {
    public final static Logger logger= (Logger) LoggerFactory.getLogger(propertiesutil.class);
    public static Properties properties;
    static {
        String filename="ftp.properties";
        properties=new Properties();
        try {
            properties.load(new InputStreamReader(propertiesutil.class.getClassLoader().getResourceAsStream(filename)));

        } catch (IOException e) {
            e.printStackTrace();
            logger.error("配置文件读取失败",e);
        }
    }
    public static String getValue(String key){
        String val=properties.getProperty(key.trim());
        if(val==null){
            return null;
        }else{
            return val.trim();
        }
    }
    public static String getValue(String key,String defval){
        String val=properties.getProperty(key.trim());
        if(val==null){
            return defval;
        }else{
            return val.trim();
        }
    }
}

  

posted @ 2019-06-21 11:05  黑魔法os  阅读(307)  评论(0编辑  收藏  举报