首页 > 其他分享 >工具类,关于手工读取 properties文件参数

工具类,关于手工读取 properties文件参数

时间:2024-09-26 16:47:50浏览次数:1  
标签:return String 手工 static import null properties 读取

 

import java.io.*;

import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;


/**
*
*
* @Package:           [cn.com.wind.Infrastructure.Core.Utils]
* @ClassName:           [PropertiesUtil]
* @Description:      
*                         [通用属性文件操作]
* @Author:           [pli.kelelipeng]
* @Date:                [2020年1月8日,上午9:49:21]
* @Version           [V1.0]
*
*/
public class PropertiesUtil {
   
   private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
   
   private static Properties props;
   static{
       loadProps();
   }

   synchronized static private void loadProps(){
          SequenceInputStream si = null;
       props = new Properties();
       try {
            //SequenceInputStream合并多个InputStream 注意第一个文件的最后一行如果没有换行符或者注释会和下一个文件第一个并列一行

               Vector<InputStream> v = new Vector<>(3);
               v.addElement(PropertiesUtil.class.getResourceAsStream("/url-param.properties"));
               v.addElement(PropertiesUtil.class.getResourceAsStream("/jdbc.properties"));
               v.addElement(PropertiesUtil.class.getResourceAsStream("/windAuth.properties"));

               Enumeration<? extends InputStream> e = v.elements();
               si = new SequenceInputStream(e);
           props.load(si);
       } catch (FileNotFoundException e) {
           logger.error("jdbc.properties文件未找到");
       } catch (IOException e) {
           logger.error("出现IOException");
       } finally {
           try {
               if(null != si) {
                         si.close();
               }
           } catch (IOException e) {
               logger.error("jdbc.properties文件流关闭出现异常");
           }
       }

   }

   public static String getProperty(String key){
       if(null == props) {
           loadProps();
       }
       return props.getProperty(key);
   }

   public static String getProperty(String key, String defaultValue) {
       if(null == props) {
           loadProps();
       }
       return props.getProperty(key, defaultValue);
   }

     /**
      * 返回资源文件的 Properties 对象.
      *
      * @param strFile
      *            传入属性文件名称串.
      * @return Properties 对象
      * @throws IOException
      */
     public static Properties getProperties(String strFile) {
          // 类装载器,仅读取类路径下文件
          InputStream inStream = ClassLoader.getSystemResourceAsStream(strFile);
          Properties properties = new Properties();
          try {
               properties.load(inStream);
               return properties;
          } catch (IOException e) {
               String errorInfo=LoggerHelper.getStackErrorInfos(e);
               LoggerHelper.LogError.error(errorInfo);
          }
          return null;
     }

     
     
     
          public static String getResourcePath2()  {
               try
               {
               return ResourceUtils.getURL("classpath:").getPath();
               }
               catch(Exception ex)
               {
                    return null;                    
               }
          }
     
     
     //在 resource目录下必须要存在的一个文件名
     private static final String resouceFile="application.properties";
     private static final String resouceFile2="application.yml";

     
     /**
      *
      * @Title: getResourcePath  
      * @Description:
      *  资源文件路径
      * @param: @return      
      * @return: String      
      * @throws
      */
     public static String getResourcePath()   {
     
          
          String classPath=null;
          try {
               
               File file=null;
               try
               {
                    //.properties
                    file= new ClassPathResource(resouceFile).getFile();
               }
               catch(Exception e)
               {     
               }
               
               if(file==null)
               {     
                    //.yml
                    file= new ClassPathResource(resouceFile2).getFile();
               }               
               classPath = file.getParent();
               
          } catch (IOException e) {               
               e.printStackTrace();
          }
          return classPath;
     }
     
}


标签:return,String,手工,static,import,null,properties,读取
From: https://www.cnblogs.com/kelelipeng/p/18433696

相关文章

  • RC522 NFC Reader,解决无法读取完整内存数据【Arduino IDE】
    目录一、前言二、项目概要三、实验测试(一)读取uid(二)读取内存与写入内存回到开头提到的两个问题:第一种情况读不全数据,//TrythempagesoftheoriginalUltralight.UltralightChasmorepages.第二种情况:直接无法读出数据,有时候甚至是一开始读得出,后来突然又读不出了......
  • java读取寄存器数据
    一:概述在嵌入式系统或硬件编程中,Java通常不是首选语言,因为它运行在虚拟机上,与硬件层面的交互不够直接。然而,随着Java技术的发展,以及JNA(JavaNativeAccess)等库的出现,使得Java也能进行一些底层操作,包括读取寄存器数据。本文将探讨几种在Java中读取寄存器数据的方法,并提供实际案例。......
  • java读取寄存器数据
    一:概述在嵌入式系统或硬件编程中,Java通常不是首选语言,因为它运行在虚拟机上,与硬件层面的交互不够直接。然而,随着Java技术的发展,以及JNA(JavaNativeAccess)等库的出现,使得Java也能进行一些底层操作,包括读取寄存器数据。本文将探讨几种在Java中读取寄存器数据的方法,并提供实际案例。......
  • 人工智能 | 手工测试用例转Web自动化测试生成
    简介在传统编写Web自动化测试用例的过程中,基本都是需要测试工程师,根据功能测试用例转换为自动化测试的用例。市面上自动生成Web或App自动化测试用例的产品无非也都是通过录制的方式,获取操作人的行为操作,从而记录测试用例。整个过程类似于但是通常录制出来的用例可用性、可维......
  • 文件读取与查看
    一、读取publicStringsetFileName(){StringBuilderstrFile=newStringBuilder();StringfilePath="你的路径";Filedirectory=newFile(filePath);//确保路径确实指向一个目录if(directory.exists()&&directory.isDirectory()){......
  • 【C#】文件读取 -- .xml文件读取
    .xml文件读取与写入[实现效果][实现编程]///<summary>///创建xml配置///</summary>///<paramname="path">path,如:x:\\xxxx\\xxx.xml</param>privatevoidcreateXmlText(stringpath){if(string.IsNullOrEmpty(path))return......
  • PHP读取文件内容的几种方法和函数
    在PHP中,读取文件内容是一个常见的操作,它可以通过多种函数和方法来实现。每种方法都有其特定的用途和性能考量。1.file_get_contents()file_get_contents()函数是最简单、最直接的方式来读取整个文件的内容到一个字符串中。这个函数对于读取较小的文件非常有效,但对于大文件......
  • 前端读取PDF和DOCX文件(干货分享)
    ⚠️⚠️文前推荐一下......
  • java浅拷贝BeanUtils.copyProperties引发的RPC异常
    背景近期参与了一个攻坚项目,前期因为其他流程原因,测试时间已经耽搁了好几天了,本以为已经解决了卡点,后续流程应该顺顺利利的,没想到人在地铁上,bug从咚咚来~没有任何修改的服务接口,抛出异常:java.lang.ClassCastException:java.util.HashMapcannotbecasttocn.xxx.xxx.xxx.xx......
  • Pandas读取保存数据
    Pandas读取保存数据学习目标能够使用Pandas读写文件中的数据知道Pandas读取数据时常用参数index_col、parse_dates、sheet_name、index的作用和用法知道Pandas和MySQL数据库进行读写交互的方法1读写文件常用读写文件函数清单【查表】无需记忆文件格式读取函数写入函......