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 ...
随机推荐
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- SQL查临时表没有返回数据集
问题描述:在SQL中可以查询到数据,返回不到页面上. 解决办法: set nocount on create table #list [转] 每次我们在使用查询分析器调试SQL语句的时候,通常会看到一 ...
- Martin Fowler’s Active Record design pattern.
P of EAA: Active Record https://www.martinfowler.com/eaaCatalog/activeRecord.html Active Record An o ...
- JavaScript正则中\1\2的作用
一.示例 1. 验证6个相同的数字 var reg = new RegExp(/^(\d)\1{5}/g); var a = '333333'; if(reg.test(a)) { alert('ri ...
- 整理前端css/js/jq常见问题及解决方法(3)
jq: 1.prepend(参数);//将参数内容前置再某元素内部; eg: <div id="div1">奇妙能力歌</div> $("#div ...
- bootstrap页面布局
首先,我们必须明确,在这四种角色登录网站,看到页面是不同,这里不仅涉及到后端的权限控制,还涉及到前端页面的布局,区分好这些角色看的东西哪些是相同的,哪些又是不同的呢,这个必须在这里想明白,所以要做好页 ...
- JDBC 入门
1. JDBC 简介 JDBC (Java DataBase Connectivity) 就是 Java 数据库连接, 说白了就是用 Java 语言向 数据库发送 SQL 语句. JDBC 其实是访问 ...
- springboot 项目跨域问题 CORS
package com.example.demo.cors; import org.springframework.context.annotation.Bean; import org.spring ...
- seaborn(matplotlib)画图,linux系统中文乱码等问题解决
data = pd.read_json(json.dumps(issue_dpl)) # set pic size plt.figure(figsize=(13, 5)) sns.set_style( ...
- bolg项目
写代码要尽可能的捕获异常 模板的路径可以直接放到TEMPLATES里面的DIRS当中,TEMPLATE_DIRS可以取消掉 设置static静态文件STATICFILES_DIRS里面,这是一个元组 ...