读取properties文件中的内容

看情况有时候某些属性值在很多地方要用,而且可能会有改动,就可以把它们存在properties文件中。

当然也可以准备一个静态类来放。

//调用方法的静态代码块
private static String getStr = "";
static {
  try {
    Properties properties = PropertiesUtil.readProperties("sys.properties");
    //Str是文件中配置的名字部分
    //例:文件中存了几个属性,其中一个是Str=test
    //那么取出来的数据中,getStr=test(取出来的都是字符串)
    getStr = properties.getProperty("Str");
  } catch (IOException e) {
    e.printStackTrace();
  }
}
//方法类
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertiesUtil {
  public static Properties readProperties(String fileName) throws IOException {
    Properties properties = new Properties();
    InputStream inStream = PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName);
    if (inStream == null) {
      throw new RuntimeException(fileName + " not found");
    }

    properties.load(inStream);
    return properties;
  }
}

原文地址:https://www.cnblogs.com/IceBlueBrother/p/8423019.html

时间: 2024-08-30 02:35:52

读取properties文件中的内容的相关文章

使用spring最简单地读取properties文件中的内容

相比传统的读取propertis文件内容,使用spring框架会更加简单快捷 1. 第一步,在spring的配置文件中,将propertis文件加载到spring容器 2. 加载了配置文件后,只需要在需要使用的地方,使用spring注入即可 原文地址:https://www.cnblogs.com/mayiaction/p/10869758.html

Flex读取txt文件中的内容报错

Flex读取txt文件中的内容 1.具体错误如下 2.错误原因 读取文件不存在 var file:File = new File(File.applicationDirectory.nativePath+"/phone.txt"); 3.解决办法 将文件导入进去 Flex读取txt文件中的内容报错

Flex读取txt文件中的内容(一)

Flex读取txt文件中的内容 phone.txt: 13000003847 13000003848 13000003849 13000003850 13000003851 13000003852 13000003853 13000003854 13000003855 13000003856 13000003857 13000003858 13000003859 13000003860 13000003861 13000003862 13000003863 13000003864 1300000

Flex读取txt文件中的内容(二)

Flex读取txt文件中的内容 自动生成的文件 LoadTxt-app.xml: <?xml version="1.0" encoding="utf-8" standalone="no"?> <application xmlns="http://ns.adobe.com/air/application/1.5.3"> <!-- Adobe AIR Application Descriptor Fi

解决在读取properties文件中出现中文报错问题

// 读取properties中的参数 Properties properties = new Properties(); // 读取properties文件 使用InputStreamReader来解决中文报错问题 InputStreamReader inputStreamReader = null; // InputStream inputStream = Main.class.getResourceAsStream("/file.properties"); // 需要遍历的路径

注解形式读取properties文件中的属性

1.spring.xml中加入(多个properties 用逗号隔开)  <context:property-placeholder location="classpath:jdbc.properties,classpath:config.properties" /> 2.spring-mvc.xml 中加入(只加上面一句controller中读取不到值) <context:property-placeholder location="classpath:c

python 参数化之读取yaml文件中的内容

一.YAML的安装: 二.YAML的基础语法: a.大小写敏感; b. 对齐缩进(注意同级缩进要保持一致,并且第一个缩进只空一个空格) 如下面试一个混合的,希望读出来是key-value形式的(开始遇到坑,基础薄弱的原因,开始第一个缩进用的是两个空格,导致结果多出来的是字符串形式,没法转为字典显示): 三.读取并解析YMAL文件 import yaml import os def readyml(yamlPath): if not os.path.isfile(yamlPath): raise

五种方式让你在java中读取properties文件内容不再是难题

一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC+Mybatis整合开发的项目中通过java程序读取properties文件内容的方式进行了梳理和分析,先和大家共享. 二.项目环境介绍 Spring 4.2.6.RELEASE SpringMvc 4.2.6.RELEASE Mybatis 3.2.8 Maven 3.3.9 Jdk 1.7 Id

Spring 中 用 ${xxx} 读取properties文件的说明

properties 如果在 spring 中通过 PropertyPlaceholderConfigurer 加载,当spring 中需要 用到 properties 中的一些 key 和value 值时可以 利用 PorpertyPlaceholderConfiger 提供的$ 直接 取得. PorpertyPlaceholderConfiger 有一些常用的属性,在一些高级应用中,可能会用到 locations fileEncoding 属性文件的编码格式 order 文件中定义了多个Pr