配置文件读取工具类--propertiesutil(代码片段)

cslj2013 cslj2013     2023-03-09     306

关键词:

 

技术图片
/**
 * 属性工具类
 * @author admin
 * 参考:https://www.cnblogs.com/doudouxiaoye/p/5693454.html
 */
public class PropertiesUtil 
    private static Properties pro=null;
    private static Map<String,Object> map=new HashMap<String, Object>();
    
    //静态块中加载
    static 
        //读取多个配置文件
        init("b.properties");//类路径下直接使用文件名,文件加载方式要全路径名
//        init("fileName2");
    
    
    //读取配置文件操作
    private static void init(String fileName) 
        try 
            pro = new Properties();
            //文件方式
//            pro.load(new FileInputStream(new File(fileName)));
            //类加载器方式
            pro.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
            
          //可遍历properties的key和value
            //方式一  key(string)集合
           /* for (String key : pro.stringPropertyNames()) 
                System.out.println(key + "=" + pro.getProperty(key));
                //存入map中
                map.put(key, pro.getProperty(key));//string
                map.put(key, pro.get(key));//对象
            */
            
          //方式二  key(对象)集合
           /* Set<Object> keys=pro.keySet();
            for (Object key : keys) 
                System.out.println(key + "=" + pro.get(key));
                //存入map中
                map.put(key.toString(), pro.get(key));
            */
            
          //方式三  键值对集合(全面)
            Set<Map.Entry<Object, Object>> entrySet = pro.entrySet();//返回的属性键值对实体
            for (Map.Entry<Object, Object> entry : entrySet) 
                System.out.println(entry.getKey() + "=" + entry.getValue());
              //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            
            //或迭代器方式
            Iterator<Entry<Object, Object>> it=entrySet.iterator();
            while(it.hasNext()) 
                Map.Entry<Object, Object> entry =it.next();
                System.out.println(entry.getKey() + "=" + entry.getValue());
                //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            
            
          //方式三  Enumeration(传统接口)
           /* Enumeration<?> e = pro.propertyNames();
            while (e.hasMoreElements()) 
                String key = (String) e.nextElement();
                String value = pro.getProperty(key);
                System.out.println(key + "=" + value);
              //存入map中
                map.put(key, value);
            */

            
          //保存属性到b.properties文件
//            FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开
//            pro.setProperty("phone", "10086");
//            pro.store(oFile, "The New properties file");
//            oFile.close();

         catch (FileNotFoundException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
         catch (Exception e) 
            e.printStackTrace();
        
    
    
    /**
     * 简单调用方式
     */
    //方式1:静态方法调用
    public static String getValue(String key) 
        return pro.getProperty(key);
    

    //方式2:静态常量调用
    public static final String FILE_NAME=pro.getProperty("fileName");
    
    public static void main(String[] args) 
        System.out.println("调用方式1 "+PropertiesUtil.pro.getProperty("fileName"));
        System.out.println("调用方式2 "+PropertiesUtil.FILE_NAME);
        System.out.println("调用方式3 "+PropertiesUtil.getValue("fileName"));
    
View Code
/**
 * 属性工具类
 * @author admin
 * 参考:https://www.cnblogs.com/doudouxiaoye/p/5693454.html
 */
public class PropertiesUtil 
    private static Properties pro=null;
    private static Map<String,Object> map=new HashMap<String, Object>();
    
    //静态块中加载
    static 
        //读取多个配置文件
        init("b.properties");//类路径下直接使用文件名,文件加载方式要全路径名
//        init("fileName2");
    
    
    //读取配置文件操作
    private static void init(String fileName) 
        try 
            pro = new Properties();
            //文件方式
//            pro.load(new FileInputStream(new File(fileName)));
            //类加载器方式
            pro.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
            
          //可遍历properties的key和value
            //方式一  key(string)集合
           /* for (String key : pro.stringPropertyNames()) 
                System.out.println(key + "=" + pro.getProperty(key));
                //存入map中
                map.put(key, pro.getProperty(key));//string
                map.put(key, pro.get(key));//对象
            */
            
          //方式二  key(对象)集合
           /* Set<Object> keys=pro.keySet();
            for (Object key : keys) 
                System.out.println(key + "=" + pro.get(key));
                //存入map中
                map.put(key.toString(), pro.get(key));
            */
            
          //方式三  键值对集合(全面)
            Set<Map.Entry<Object, Object>> entrySet = pro.entrySet();//返回的属性键值对实体
            for (Map.Entry<Object, Object> entry : entrySet) 
                System.out.println(entry.getKey() + "=" + entry.getValue());
              //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            
            //或迭代器方式
            Iterator<Entry<Object, Object>> it=entrySet.iterator();
            while(it.hasNext()) 
                Map.Entry<Object, Object> entry =it.next();
                System.out.println(entry.getKey() + "=" + entry.getValue());
                //存入map中
                map.put(entry.getKey().toString(), entry.getValue());
            
            
          //方式三  Enumeration(传统接口)
           /* Enumeration<?> e = pro.propertyNames();
            while (e.hasMoreElements()) 
                String key = (String) e.nextElement();
                String value = pro.getProperty(key);
                System.out.println(key + "=" + value);
              //存入map中
                map.put(key, value);
            */

            
          //保存属性到b.properties文件
//            FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开
//            pro.setProperty("phone", "10086");
//            pro.store(oFile, "The New properties file");
//            oFile.close();

         catch (FileNotFoundException e) 
            e.printStackTrace();
         catch (IOException e) 
            e.printStackTrace();
         catch (Exception e) 
            e.printStackTrace();
        
    
    
    /**
     * 简单调用方式
     */
    //方式1:静态方法调用
    public static String getValue(String key) 
        return pro.getProperty(key);
    

    //方式2:静态常量调用
    public static final String FILE_NAME=pro.getProperty("fileName");
    
    public static void main(String[] args) 
        System.out.println("调用方式1 "+PropertiesUtil.pro.getProperty("fileName"));
        System.out.println("调用方式2 "+PropertiesUtil.FILE_NAME);
        System.out.println("调用方式3 "+PropertiesUtil.getValue("fileName"));
    

 

mendmix代码解析:百搭的配置文件读取工具resourceutils(代码片段)

...候,我们都习惯在项目里面写一个类似ResourceUtils或者PropertiesUtil的工具,无论在静态方法还是jsp代码都屡试不爽。如今Springcloud各种参数化配置、各种profile,Apollo、Nacos各种配置中心以及Properties、Yaml各种配置格式你... 查看详情

配置文件读取的封装

将配置文件放到src下即可 原理:获取src目录下的文件一个个读取.properties后缀的文件publicclassPropertiesUtil{publicstaticvoidmain(String[]args){System.out.println(PropertiesUtil.getValue("mail.username"));}privatestaticProperties 查看详情

android对.properties文件的读取

...Stringfilepath)Filefile=newFile(filepath);if(file.exists())Strings=PropertiesUtil.readValue(filepath,"allTime");if(s!=null)ShowAllTime=Integer.parseInt(s)*60*1000;Stringmqtt=PropertiesUtil.readValue(filepath,"showTime");if(mqtt!=null)ShowTime=Integer.parse... 查看详情

propertiesutil读取properties

packagecom.yun.util;importjava.io.InputStream;importjava.io.UnsupportedEncodingException;importjava.util.Enumeration;importjava.util.HashMap;importjava.util.Map;importjava.util.Properties;importorg.sl 查看详情

jdbc工具类(代码片段)

...FunctionalInterfacepublicinterfaceIRowMappervoidmapRow(ResultSetresultSet);PropertiesUtil类:packagecom.jd.util;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;publicclassPropertiesUtilprivatestaticPropertiesproperties=newProperties();staticInputStreaminputS... 查看详情

java读取配置文件

推荐:使用Spring自带工具类ClassUtils读取配置文件/***初始化配置redis文件*@paramconfigFileName*/privatevoidinitProperties(StringconfigFileName){try{InputStreamresourceAsStream=ClassUtils.getDefaultClassLoader().getResourceAsS 查看详情

读取properties文件中的内容

...atestaticStringgetStr="";static{  try{    Propertiesproperties=PropertiesUtil.readProperties("sys.propertie 查看详情

csvfileutil读取写入csv文件简单工具类(代码片段)

参考github大神源码总结一下最简单的工具类记录一下/***@descriptionCSV文件读取和输出工具类.<br/>*@authormichael*@date2019/05/16*@versionCopyright(c)2019,[email protected]AllRightsReserved.*/publicclassCSVFileUtilprivatesta 查看详情

基于python封装读取ini文件的工具类python+requests库做接口自动化框架设计系列多测师(代码片段)

...9Company:上海多测师信息有限公司===========================""""""配置文件类的封装封装的目的:使用更简单封装的需求:1、简化创建配置文件解析器对象,加载配置文件的流程(需要 查看详情

通过配置文件的key得值(代码片段)

publicstaticStringTM_API_SERVER_HOST=PropertiesUtil.getProperty("config/im.properties","TIM_HOST");publicstaticStringgetProperty(Stringresource,Stringkey)Propertiesproperties=PropertiesUtil.getProper 查看详情

java常用工具类——文件读取的操作类(代码片段)

定义常用的文件类型publicclassFileType/***文件头类型*/publicstaticfinalStringXML_FILE="text/xml;charset=UTF-8";publicstaticfinalStringPDF_FILE="application/pdf";publicstaticfinalStringPDG_FILE="application/x-png";p 查看详情

使用embeddedvalueresolveraware读取配置文件内容

...取properties文件属性值的时候,一般使用@Value的方式注入配置文件属性值,但是总是需要引入这些多余的变量,有点不爽,今天研究了下,基于Spring解析@Value的方式,使用EmbeddedValueResolverAware解析配置文件,实现起来也很简单工具... 查看详情

properties文件读取工具类(代码片段)

...止硬编码,经常会将一些与业务相关的数据存在properties文件中,根据不同的key加载不同的数据,所以就会有读取properties文件的东西,这篇文章仅为了以后copy方便写的。1.添加依赖:<!--https://mvnrepository.com/artifact/commons-configurati... 查看详情

9hutool实战:fileutil文件工具类(读取文件)(代码片段)

...xff08;带你掌握里面的各种工具)目录用途:FileUtil文件工具类(读取文件)使用场景读取文件内容的各种骚操作项目引用此博文的依据:hutool-5.6.5版本源码<dep 查看详情

java读取.txt文件工具类fileutiles(代码片段)

publicclassFileUtilsprivatestaticfinalStringENCODING="UTF-8";//编码方式/***获取文件的行**@paramfileName*文件名称*@returnList<String>*/publicstaticStringgetContentByLine(StringfileName)StringBufferlines=newS 查看详情

spark小文件异步合并工具类

...个能避免读写冲突的小文件合并工具。TBC:可通过读取MySQL配置表来指定需要合并的目录、文件类型,方便随时修改。 查看详情

java读取properties文件工具类并解决控制台中文乱码

...bsp;  2、在spring-mvc.xml文件(applicationContext-mvc.xml)中配置properties工具类路径及读取properties文件的路径 <beanid="propertyConfigurer"class="com.yjlc.platform.utils.PropertyConfigurer"><propertyname="ignoreUnresolvablePlaceholders"value="true"/><... 查看详情

java获取配置文件路径

...里面怎么获取文件的路径。使用getResource的路径为空读取配置文件,xxx.properties放在webroot/WEB-INF/classes/目录下首先将配置文件转换成InputStream,有两种方式,原理一样,都是通过类加载器得到资源:(1)InputStreaminputStream=Thread.current... 查看详情