读取配置文件中的数据
2010-06-08 17:04
295 查看
在java中有时候会将一些数据放在配置文件中,每次使用的时候进行读取。
1.工具类,获取配置文件。
2.类中调用直接一句话就可以了,$(name)配置文件中的属性名
PropertyUtil.getPeopertyValueByName("$(name)");
3.配置文件为data.properties,内容格式为 $(name) = $(value)
如 name = China
不用加标点,属性值取当前行 “=” 后面的内容。
1.工具类,获取配置文件。
package com.stduy.Util; import java.io.FileInputStream; import java.util.Properties; /** * 读配置文件中的数据 * */ public class PropertyUtil { /** * 得到配置文件对象 */ private static Properties getProperty() { Properties property; try { String context; if (isLinux()) { context = PropertyUtil.class.getClassLoader().getResource("/") .getFile(); } else { context = PropertyUtil.class.getClassLoader().getResource("") .getFile(); } FileInputStream inputFile = new FileInputStream(context + "data.properties"); property = new Properties(); property.load(inputFile); inputFile.close(); } catch (Exception e) { e.printStackTrace(); return null; } return property; } /** * 根据属性名称读取属性值 */ public static String getPeopertyValueByName(String name) { String value = null; value = getProperty().getProperty(name); return value; } /** * 判断操作系统 */ public static boolean isLinux() { if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) { return false; } else { return true; } } }
2.类中调用直接一句话就可以了,$(name)配置文件中的属性名
PropertyUtil.getPeopertyValueByName("$(name)");
3.配置文件为data.properties,内容格式为 $(name) = $(value)
如 name = China
不用加标点,属性值取当前行 “=” 后面的内容。
相关文章推荐
- Dropdownlist统一配置(从xml或枚举读取数据),应用文件依赖缓存.
- Yii2.0 安装使用报错:cookieValidationKey和读取配置文件中的数据
- c#读取ini配置文件、将配置数据保存至ini文件
- python 从两个配置文件读取数据,形成一个url思路代码
- 向空文件写数据,并读取配置文件数据
- ZendFramework学习第三章(核心组件—使用配置数据之从ini配置文件中读取数据)
- ZendFramework学习第三章(核心组件—使用配置数据之从XML配置文件中读取数据)
- 从配置文件里读取数据,实现多数据库连接
- 封装properties从配置文件读取测试用例输入数据
- ADO.NET数据库编程 利用应用程序配置文件进行数据的读取
- 如何读取配置文件数据
- java 读取和写入配置文件中的数据
- 从配置文件里读取数据,实现多数据库连接
- 反射的应用,读取properties配置文件中的数据(普通InputStream方式,类加载方式),再调用运行
- 学习:C#读取ini配置文件数据
- 将配置文件中的数据读取到JAVA代码中使用
- 基于ini配置文件实现多维数组数据的按行存储和读取
- ArrayList 与HashSet的比较,及应用反射读取properties配置文件中的数据进行实例化再调用,以及类加载器的使用;还有HashCode的分析,及导致内存泄露,内存溢出的原因之一
- C# 定义配置文件 使程序在配置文件中读取数据
- 从配置文件中读取数据获取Connection