通常我们就会看到一个配置文件,比如: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. JavaScript严格模式为何要禁用With语句

    看了很多遍JavaScript严格模式,其中有说“禁用With语句”,以前看到这都是骑马观花,一带而过,因为平时就很少用到这个语句,禁不禁用对自己关系都不是很大.今天禁不住想知道为何“严格模式”就容不 ...

  2. Splitting Pile

    Splitting Pile Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Snuke a ...

  3. GridView中给DropDownList动态绑定数据,及选择列表值后自动更新数据库

    protected void sgvFile1_RowDataBound(object sender, GridViewRowEventArgs e) { DropDownList ddlAM = ( ...

  4. 迷宫问题(java实现)

    1. public class Direction { private int x; private int y; public Direction(int x, int y) { this.x = ...

  5. jPage.js分页

    jPage.js插件使用文档 这一款插件主要是为了bootstrap原生的分页功能效果不理想而诞生的一款插件. jPage.js代码更新地址为:https://github.com/leslieSie ...

  6. Python星号*与**用法分析 What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 必选参数 默认参数 可变参数 关键字参数

    python中*号**的区别 - CSDN博客 https://blog.csdn.net/qq_26815677/article/details/78091452 定义可变参数和定义 list 或 ...

  7. Windows下批处理文件(.bat)的使用

    cmd文件和bat文件的区别:在本质上两者没有区别,都是简单的文本编码方式,都可以用记事本创建.编辑和查看.两者所用的命令行代码也是共用的,只是cmd文件中允许使用的命令要比bat文件多.cmd文件只 ...

  8. go语言之并发编程同步一

    前面介绍了采用go语法的并行操作以及channel.既然是并行操作,那么就涉及到数据原子性以及同步的问题.所以在Go里面也需要采用同步的机制. 互斥锁: 由标准库代码包sync中的Mutex结构体类型 ...

  9. Ubuntu Server 16.04安装xfce4图形界面远程控制

    1.首先连接上你的服务器,然后安装vncserver,命令如下 apt-get install vnc4server 2.安装图形界面 apt-get install xfce4如果安装不上,就 ap ...

  10. ABAP权限检查,TCode与权限对象进行关联

    一.确认权限对象,和关联字段: Tcode:SU21 维护权限对象例如"M_MSEG_WMB",它关联字段为'WERKS'M_MSEG_WMB 物料凭证:工厂 二.在ABAP代码中 ...