通常我们就会看到一个配置文件,比如:jdbc.properties,它是以“.properties”格式结尾的。在java中,这种文件的内容以键值对<key,value>存储,通常以“=”分隔key和value,当然也可以用":"来分隔,但通常不这么干。

  • 读取配置文件

  这里有一个文件叫asfds.properties,里面简单的存了两个键值对,如下图所示:

  

  读取配置文件的基本步骤是:

  1. 实例化一个Properties对象;
  2. 将要读取的文件放入数据流中;
  3. 调用Properties对象的load方法,将属性文件的键值对加载到Properties类对象中;
  4. 调用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(); }
  • 写入配置文件

  写入配置文件的基本步骤是:

  1. 实例化一个Properties对象;
  2. 获取一个文件输出流对象(FileOutputStream);
  3. 调用Properties对象的setProperty(String key,String value)方法设置要存入的键值对放入文件输出流中;
  4. 调用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配置文件的读写的更多相关文章

  1. JSP+Java+properties+FileInputStream文件读写,JSP页面读取properties文件

    String realPath = request.getRealPath("WEB-INF/classes/com/properties/devicetype.properties&quo ...

  2. Java properties配置文件工具类

    /* * Copyright (c) 2017. Panteng.Co.Ltd All rights reserved */ import org.apache.log4j.Logger; impor ...

  3. Java properties配置文件

    Java中的配置文件常为properties文件,格式为文本文件,文件内容的格式是“键=值”格式.注释信息使用“#”来注释. Properties类的常用方法 String getProperty(S ...

  4. java 顺序 读写 Properties 配置文件

    java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...

  5. 【转】Java 读写Properties配置文件

    [转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...

  6. Java 读写Properties配置文件

    Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...

  7. java 顺序 读写 Properties 配置文件 支持中文 不乱码

    java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...

  8. (转)Java 读写Properties配置文件

    原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...

  9. java读写properties配置文件方法

    1.Properties类 Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串. 虽然Properties类继承了j ...

随机推荐

  1. 160819、JavaScript-数组去重由慢到快由繁到简

    JavaScript-数组去重由慢到快由繁到简演化   indexOf去重 Array.prototype.unique1 = function() { var arr = []; for (var ...

  2. 巨蟒python全栈开发flask1

    1.整体把握 (1)路飞学城 - RestAPI 前后端分离开发 Django Vue.js - DRF DjangoRestFromwork - 线上商城的经验 (2)智能玩具 - RestAPI ...

  3. 禁止向 HTML 页面输出未经安全过滤或未正确转义的用户数据。

    https://github.com/alibaba/p3c/blob/master/阿里巴巴Java开发手册(详尽版).pdf 5. [强制]禁止向 HTML 页面输出未经安全过滤或未正确转义的用户 ...

  4. http-proxy-middleware使用方法和实现原理(源码解读)

    本文主要讲http-proxy-middleware用法和实现原理. 一 简介 http-proxy-middleware用于后台将请求转发给其它服务器. 例如:我们当前主机A为http://loca ...

  5. Linux Debian 如何部署 Qt?

    Linux Debian 如何部署 Qt? 在这里以 HelloWorld 为例 目录结构如下: . ├── HelloWorld ├── HelloWorld.sh ├── imageformats ...

  6. 介绍一下Python中webbrowser的用法?

    webbrowser模块提供了一个高级接口来显示基于Web的文档,大部分情况下只需要简单的调用open()方法.webbrowser定义了如下的异常:exception webbrowser.Erro ...

  7. 004-ibus输入法,快捷键,浏览器

    一.输入法 用 root 身份在终端下,运行下面命令: yum install ibus-pinyin ibus ibus-gtk ibus-qt 使用im-chooser命令,选择ibus为默认输入 ...

  8. 爬虫二 requests模块的使用

    一.requests模块的介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...

  9. or and 运算符与 pyhton编码

    运算符 # x or y 如果 x 为真,则值为x,否则为y 1 print(4 or 3) # 4 2 print(2 or 3) # 2 3 print(1 or 3) # 1 4 print(0 ...

  10. Android sdk manager加载缓慢或加载不出来

    1.打开android sdk manager 2.打开tool->options,如图所示 3.将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy P ...