package org.jasig.cas.a4.tools; import java.io.InputStream; import java.net.URL; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class UtilProperties { private static Map propsCache=new HashMap(); private static Log logger=LogFactory.getLog(UtilProperties.class); public static Properties getProperties(String resourceName){ Properties props=(Properties)propsCache.get(resourceName); UtilProperties utilProperties = new UtilProperties(); if(props==null){ props = new Properties(); ClassLoader loader=null; if (loader == null) { try { loader = Thread.currentThread().getContextClassLoader(); } catch (SecurityException e) { loader = utilProperties.getClass().getClassLoader(); } } URL url=loader.getSystemResource(resourceName); InputStream in = null; try { in = url.openStream(); props.load(in); } catch (Exception e) { try{ in=utilProperties.getClass().getResourceAsStream("/"+resourceName); props.load(in); }catch(Exception e1){ logger.error(" Couldn't find the URL: " + resourceName+e1); return props; } } } return props; } public static String getProperty(String resourceName,String key,String defaultValue){ Properties props=getProperties(resourceName); return props.getProperty(key, defaultValue); } public static String getProperty(String resourceName,String key){ return getProperty(resourceName,key, ""); } public static Map getMatchPropertys(String resourceName,String inkey){ Map result = new HashMap(); Properties props=(Properties)propsCache.get(resourceName); UtilProperties utilProperties = new UtilProperties(); if(props==null){ props = new Properties(); ClassLoader loader=null; if (loader == null) { try { loader = Thread.currentThread().getContextClassLoader(); } catch (SecurityException e) { loader = utilProperties.getClass().getClassLoader(); } } URL url=loader.getSystemResource(resourceName); InputStream in = null; try { in = url.openStream(); props.load(in); } catch (Exception e) { try{ in=utilProperties.getClass().getResourceAsStream("/"+resourceName); props.load(in); }catch(Exception e1){ logger.error(" Couldn't find the URL: " + resourceName+e1); return result; } } } Enumeration en = props.propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); if(key.contains(inkey)){ String Property = props.getProperty (key); result.put(key, props.getProperty(key, "")); } } return result; } }
调用:
private final String baseuri = UtilProperties.getProperty("runtime.properties", "cu.cloud.v1");
相关推荐
java对properties的操作工具类 属性文件
1.OutputOrderProperties p = new OutputOrderProperties(); 2.FileInputStream fs = new FileInputStream("文件路径"); 3.p.load(new InputStreamReader(fs, "utf-8")); 4.p.put("jdbc.name", dbConfig.getJdbcName...
CommentedProperties代码 博文链接:https://brokendreams.iteye.com/blog/1932875
下载了就可以直接用。默认路径在项目随意source目录下,获取时直接PropertiesUtil.getInstance().getPropertyValue("xx.properties", "value");
读取properties文件工具类。使用时直接将工具类放到Java项目中,调用读取properties文件的方法即可。
此工具类只用于Java后端在操作Properties文件的时候写的工具类,方便properties文件的存取操作
Java读取、修改utils.properties工具类,可直接拿去用。
读取Properties信息工具类,其中main方法中可以直接进行测试
通过程序生成properties文件的工具类,并可根据添加顺序生成,可添加注释,调用步骤如下: 1.OutputOrderProperties p = new OutputOrderProperties(); 2.FileInputStream fs = new FileInputStream("文件路径"); 3....
工具类里分读取.yml工具类和.properties工具类,结合博客描述使用,用不到的方法可根据个人情况删除,
NULL 博文链接:https://mov-webhobo.iteye.com/blog/1786416
一个自定义的读取properties工具类,对有需要操作properties非常有用
java连接SqlServer完整代码,工具类,jdbc JDBC工具类的构建 1.资源释放 见https://mp.csdn.net/postedit/86577443 2.驱动防二次注册 ``` Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //...
NULL 博文链接:https://fanshuyao.iteye.com/blog/2316349
字符串工具类/数据类型转换类/集合工具类/数组工具类/Properties文件操作类/常用流操作工具类/编码工具类/Json工具类/日期工具类/下载文件工具类/解压ZIP工具类/文件编码转码
RabbitMQClientUtil是MQ的测试工具类,他封装了fanout、direct、topic三种exchange模式,并包括发送数据和接收数据。 Test1、Test2是测试类 使用maven管理,在pom.xml文件中引入如下代码: <!-- Rabbitmq工具包...
整合支付宝 登陆、支付、获取用户信息等功能的一个工具类,将参数放在properties配置文件中,所有功能统一调用。货真价实,绝对好用~~
最近面试java开发,遇到很是蛋疼一道题。题目是写一个java程序批量读取properties文件的数据,按照每列属性每行每行读出来,由于很久都没写过工具类 回来复习了一下简单写了个工具类有兴趣可以看看。