zoukankan      html  css  js  c++  java
  • Java中Properties类的操作

    Java中Properties类的操作

     =============================================

    com.gordon.constant

    Constant.java:

    package com.gordon.constant;
    
    public interface Constant {
    	
    	/**
    	 * 根据名称读取常量
    	 * @param name
    	 * @return
    	 */
    	public String get_constant(String name);
    }
    

    SystemConstant4PropImpl.java

    package com.gordon.constant;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class SystemConstant4PropImpl implements Constant {
    
    	public Properties prop = null;
    
    	public SystemConstant4PropImpl() {
    		InputStream in = ClassLoader.getSystemResourceAsStream("constant.properties");
    		prop = new Properties();
    		try {
    			prop.load(in);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	public SystemConstant4PropImpl(String prop_path) {
    		InputStream in = ClassLoader.getSystemResourceAsStream(prop_path);
    		prop = new Properties();
    		try {
    			prop.load(in);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * 获取properties文件中的属性
    	 */
    	@Override
    	public String get_constant(String name) {
    		return prop.getProperty(name);
    	}
    }
    

    SystemConstant4XMLImpl.java

    package com.gordon.constant;
    
    public class SystemConstant4XMLImpl {
    	
    }
    

    com.grodon.test

    TestConstantClass.java:

    package com.gordon.test;
    
    import org.junit.Test;
    
    import com.gordon.constant.SystemConstant4PropImpl;
    
    public class TestConstantClass {
    	@Test
    	public void run() {
    		try {
    			SystemConstant4PropImpl scp = new SystemConstant4PropImpl();
    			String db_username = scp.get_constant("db.username");
    			System.out.println(db_username);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    constant.properties:

    db.username=root
    

    运行结果:

  • 相关阅读:
    网站无脑搭建,自己可以建个站玩一玩儿
    Python2和Python3中urllib库中urlencode的使用注意事项
    图像的缩放与图像金字塔
    模糊聚类算法(FCM)
    java调用python脚本
    雷林鹏分享:jsp HTTP 状态码
    雷林鹏分享:jsp 服务器响应
    雷林鹏分享:jsp 客户端请求
    雷林鹏分享:jsp 隐式对象
    雷林鹏分享:jsp 动作元素
  • 原文地址:https://www.cnblogs.com/hfultrastrong/p/7611536.html
Copyright © 2011-2022 走看看