JAVA Properties配置文件的读写
通常我们就会看到一个配置文件,比如:jdbc.properties,它是以“.properties”格式结尾的。在java中,这种文件的内容以键值对<key,value>存储,通常以“=”分隔key和value,当然也可以用":"来分隔,但通常不这么干。
- 读取配置文件
这里有一个文件叫asfds.properties,里面简单的存了两个键值对,如下图所示:

读取配置文件的基本步骤是:
- 实例化一个Properties对象;
- 将要读取的文件放入数据流中;
- 调用Properties对象的load方法,将属性文件的键值对加载到Properties类对象中;
- 调用Properties对象的getProperty(String key)读入对应key的value值。
注:如果想要读取key值,可以调用Properties对象的stringPropertyNames()方法获取一个set集合,然后遍历set集合即可。
读取配置文件的方法:
/**
* read properties file
* @param paramFile file path
* @throws Exception
*/
public static void inputFile(String paramFile) throws Exception
{
Properties props=new Properties();//使用Properties类来加载属性文件
FileInputStream iFile = new FileInputStream(paramFile);
props.load(iFile); /**begin*******直接遍历文件key值获取*******begin*/
Iterator<String> iterator = props.stringPropertyNames().iterator();
while (iterator.hasNext()){
String key = iterator.next();
System.out.println(key+":"+props.getProperty(key));
}
/**end*******直接遍历文件key值获取*******end*/ /**begin*******在知道Key值的情况下,直接getProperty即可获取*******begin*/
String user=props.getProperty("user");
String pass=props.getProperty("pass");
System.out.println("\n"+user+"\n"+pass);
/**end*******在知道Key值的情况下,直接getProperty即可获取*******end*/
iFile.close(); }
- 写入配置文件
写入配置文件的基本步骤是:
- 实例化一个Properties对象;
- 获取一个文件输出流对象(FileOutputStream);
- 调用Properties对象的setProperty(String key,String value)方法设置要存入的键值对放入文件输出流中;
- 调用Properties对象的store(OutputStream out,String comments)方法保存,comments参数是注释;
写入配置文件的方法:
/**
*write properties file
* @param paramFile file path
* @throws IOException
*/
private static void outputFile(String paramFile) throws IOException {
///保存属性到b.properties文件
Properties props=new Properties();
FileOutputStream oFile = new FileOutputStream(paramFile, true);//true表示追加打开
props.setProperty("testKey", "value");
//store(OutputStream,comments):store(输出流,注释) 注释可以通过“\n”来换行
props.store(oFile, "The New properties file Annotations"+"\n"+"Test For Save!");
oFile.close();
}
- 测试输出
文件读取:
@Test
public void testInputFile(){//read properties file
try {
inputFile("resources/asfds.properties");
} catch (Exception e) {
e.printStackTrace();
}
}
输出:

文件写入:
@Test
public void testOutputFile(){//write properties file
try {
outputFile("resources/test.properties");
} catch (Exception e) {
e.printStackTrace();
}
}
写入的文件:

JAVA Properties配置文件的读写的更多相关文章
- JSP+Java+properties+FileInputStream文件读写,JSP页面读取properties文件
String realPath = request.getRealPath("WEB-INF/classes/com/properties/devicetype.properties&quo ...
- Java properties配置文件工具类
/* * Copyright (c) 2017. Panteng.Co.Ltd All rights reserved */ import org.apache.log4j.Logger; impor ...
- Java properties配置文件
Java中的配置文件常为properties文件,格式为文本文件,文件内容的格式是“键=值”格式.注释信息使用“#”来注释. Properties类的常用方法 String getProperty(S ...
- java 顺序 读写 Properties 配置文件
java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...
- 【转】Java 读写Properties配置文件
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
- Java 读写Properties配置文件
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
- java 顺序 读写 Properties 配置文件 支持中文 不乱码
java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...
- (转)Java 读写Properties配置文件
原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...
- java读写properties配置文件方法
1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...
随机推荐
- Sql注入_mysql权限入侵
实验:测试不同数据库用户的操作权限 文件读写测试:load_file() ,into outfile 数据库用户账号密码存储在mysql.user下 Mysql最高权限用户root: Mysql普通权 ...
- Bean\Entity\Model\POJO\Dto\EJB简单解析
一.Bean 对于Bean而言,只要是Java的类的就可以称为一个Bean, 更用在Spring上,被Spring管理的对象就可以将其称作为Bean. 它不仅仅可以包括对象的属性以及get,set方法 ...
- IBM WebSphere cannot start in RAD 9.1
Have solved the problem. Solutions follows Step 1: double click on "WebSphere Application Serve ...
- javascript实例:两种方式实现tab栏选项卡
方法1: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- F110的几个功能
1.F-59, 没有找到函数, 使用BDC BAPI_ACC_DOCUMENT_POST 必须创建有借贷2 line 的凭证,需求要参考原始的SA类型凭证, 创建一个单条的 科目 = 供应商 的凭证, ...
- orange安装文档
一.Orange简介 Orange是一个基于 OpenResty/Nginx 的 API Gateway,提供 API 及 “自定义规则” 的监控和管理,如访问统计.流量切分.AB 测试.API ...
- Django的Model继承abstract,proxy,managed。。。
Django 中的 model 继承和 Python 中的类继承非常相似,只不过你要选择具体的实现方式:让父 model 拥有独立的数据库:还是让父 model 只包含基本的公共信息,而这些信息只能由 ...
- 【HTTP】WEB机器人
<HTTP权威指南>学习摘要 Web Robot的自活跃(self-animating)用户代理. Web机器人是在不需要人工干预的情况下,自动进行一系列Web事务处理的软件程序,别名“爬 ...
- list列表、tuple元组、range常用方法总结
list 列表(数组),是可迭代对象,列表是可变的所以列表的方法都是在列表本身更改的.里面看可以放各种数据类型的数据,可存储大量数据 连接列表可以使用 + 或 extend() a = [1, 3, ...
- iOS11 仿大标题 导航栏
iOS11 SytleTitleController 仿大标题 风格 导航栏 仿 iOS11 大导航标题 风格 UI 适用范围 iOS8 + 前言 iOS11全面应用大标题设计,(岂止于大—— 比逼 ...