java使用Properties类读取配置文件信息
2016-07-06 13:38
537 查看
java中使用Properties类可以方便的读取配置信息。首先需要创建一个Properties类的实例,然后使用load方法在入FileInputStream实例指向的配置文件。
假定我们要读取的配置文件内容如下:
代码:
下面是输出:
假定我们要读取的配置文件内容如下:
# System configuration # Comments will automatically be excluded by the program. parameter1=value1 parameter2=value2
代码:
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; /** * Main.java * * @author outofmemory.cn */ public class Main { Properties config; /** * Loads configuration parameters from a textfile and print them out. */ public void loadConfigFile() { //Load configuration file String filename = "conf/systemconfig.txt"; config = new Properties(); try { config.load(new FileInputStream(filename)); } catch (FileNotFoundException ex) { ex.printStackTrace(); return; } catch (IOException ex) { ex.printStackTrace(); return; } //Print out the configuration parameters Enumeration en = config.keys(); System.out.println("********** System configuration **********"); while (en.hasMoreElements()) { String key = (String) en.nextElement(); System.out.println(key + " => " + config.get(key)); } } /** * Starts the program * * @param args the command line arguments */ public static void main(String[] args) { new Main().loadConfigFile(); } }
下面是输出:
********** System configuration ********** parameter2 => value2 parameter1 => value1
相关文章推荐
- MyEclipse & eclipse 安装 Freemarker Editor 插件
- java 设计模式——结构型模式(1.适配器模式)
- Spring jar包详解
- [2016/07/06] LeetCode / Java - Day 13 -
- Java中如何获取Spring中配置的bean
- MVP中RxJava的使用
- java-ContextClassLoader的意义
- Java基础进阶整理
- java1.4升级到java1.5 新特性
- mybatis-spring不能加载属性文件内容报:Cannot load JDBC driver class ${jdbc.driverClassName}'
- [JAVA]计算两个日期相差的天数
- Struts2常用标签
- 冒泡排序——java
- 超越之MongDB系列教程(九)MongDB的java驱动与Spring的整合
- Java 日期工具类 (可以直接调用)
- Java 关系运算符、逻辑运算符和三元运算符,以及运算符优先级
- JAVA程序开发参考手册
- java环境配置(win10)
- 使用优先队列实现游戏公告模块(Java)
- JAVA服务器端获取客户端远程地址,根据IP获取远程地址,各IP地址查询接口比较