`

spring PropertiesLoaderUtils操作Properties文件

 
阅读更多
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesUtil {

private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);

    private static Properties properties= new Properties();
   
    /*properties文件名*/
    private static final String PROPERTIES_FILE_NAME="centrin-dao.properties";
   
   
    public static String getProperty(String name) {
      String result = null;
   
      try {
       //下载文件
      properties = PropertiesLoaderUtils.loadAllProperties(PROPERTIES_FILE_NAME);
       result = properties.getProperty(name);//根据name得到对应的value
      } catch (IOException e) {
       logger.warn(" loadAllProperties  error 1");
       e.printStackTrace();
      }
      return result;

    }
   
    public static String getProperty(String name ,String properties_name) {
    String result = null;
 
    try {
     //下载文件
  properties = PropertiesLoaderUtils.loadAllProperties(properties_name);
     result = properties.getProperty(name);//根据name得到对应的value
    } catch (IOException e) {
     logger.warn(" loadAllProperties  error 1");
     e.printStackTrace();
    }
    return result;

  }
    public static void main(String[] args) throws IOException {
   
   
    System.out.println(PropertiesUtil.getProperty("createPlanPath"));
       
    }
}
分享到:
评论

相关推荐

    JAVA 解决Properties文件保存中文乱码

    在Java编程中,Properties文件是用于存储配置信息的文本文件,通常包含键值对,其中键和值可以是任意字符串。然而,当这些文件中包含中文字符时,如果不正确地处理编码,可能会出现中文乱码的问题。本文将深入探讨...

    详解spring boot 使用application.properties 进行外部配置

    application.properties 文件是 Spring Boot 中的一个默认配置文件,用于存储可以手动修改的变量,以便在生产环境中不需要重新编译代码。 Spring Boot 的默认配置信息 Spring Boot 提供了许多默认的环境变量,例如...

    PropertiesLoaderUtils 出现中文乱码的解决方式

    PropertiesLoaderUtils 是 Spring 框架中的一种 properties 加载工具类,用于加载 properties 文件中的配置信息。然而,在使用 PropertiesLoaderUtils 加载 properties 文件时,经常会遇到中文乱码的问题。本文将...

    java读取配置文件

    Properties props = PropertiesLoaderUtils.loadProperties(resource); String dbUrl = props.getProperty("db.url"); ``` 5. **使用`java.nio`包** Java NIO(New IO)提供了非阻塞的I/O操作,可以更高效地...

    使用spring工厂读取property配置文件示例代码

    Spring XML配置文件中定义了一个`PropertyPlaceholderConfigurer`bean,该bean负责加载配置文件(如`props/${property-path}.properties`和`important.properties`),并处理占位符替换。 接下来,我们看看第二种...

    Spring项目里将SQL语句写在.sql文件中的方法

    在这个实现中,我们首先使用`PropertiesLoaderUtils.fillProperties`加载.sql文件的内容到`Properties`对象。然后,遍历`Properties`,筛选出以`--!`开头的键,并将其值放入一个`Map`中。最后,创建一个`...

    GetYamlUtil.rar

    import org.springframework.core.io.support.PropertiesLoaderUtils; import org.yaml.snakeyaml.Yaml; import java.io.IOException; import java.util.Map; public class GetYamlUtil { public static Map, ...

Global site tag (gtag.js) - Google Analytics