Java读取properties配置文件
一.创建properties配置文件
file_upload_path=http://localhost:8011/ecps-file solr_path=http://localhost:8080/solr deploy_html_path=C:/Users/user/workspac/gxshop1/ecps-parent0317/ecps-portal/src/main/webapp/html ws_pass=123 portal_path=http://localhost:8021/ecps-portal request_file_path=http://localhost:8011/ecps-file host_path=192.168.43.192 port_path=6379 key_val=key_val
二.创建util工具类
package util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ECPSUtils { public static String getProp(String key){ //定义一个临时的值变量 String value = ""; //创建properties对象 Properties p = new Properties(); //类加载器(读取配置文件) InputStream is = ECPSUtils.class.getClassLoader().getResourceAsStream("ecps_prop.properties"); try { //从输入流中读取属性列表(键值对) p.load(is); //通过形式参数的key获取值 value = p.getProperty(key); //关闭流 is.close(); } catch (IOException e) { e.printStackTrace(); } return value; } }
三.创建测试类
package test; import util.ECPSUtils; public class TestProperties { public static void main(String[] args) { String file_upload_path = ECPSUtils.getProp("file_upload_path"); System.out.println(file_upload_path); } }
四.结果