通常我们就会看到一个配置文件,比如: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. hdu1174(3维射线与圆是否相交)

    简单的题意,要注意z2 = h2*0.9-r2 #include <iostream> #include <cmath> #include <vector> #in ...

  2. [Spring Data MongoDB]学习笔记--MongoTemplate查询操作

    查询操作主要用到两个类:Query, Criteria 所有的find方法都需要一个query的object. 1. 直接通过json来查找,不过这种方式在代码中是不推荐的. BasicQuery q ...

  3. EasyPlayer-RTSP-Android安卓播放器播放RTSP延迟优化策略,极低延时!

    EasyPlayer-RTSP-Android安卓RTSP播放器低延迟播放延时优化策略 EasyPlayer-RTSP-Android播放器是一款专门针对RTSP协议进行过优化的流媒体播放器,其中我们 ...

  4. SQL Server函数​

    阅读目录 SQL Server函数---Union与Union All的区别 回到顶部 SQL Server函数---Union与Union All的区别 如果我们需要将两个select语句的结果作为 ...

  5. 自定义表单验证--jquery validator addMethod的使用

    原文地址:jquery validator addMethod 方法的使用作者:蜡笔小玄 jQuery.validate是一款非常不错的表单验证工具,简单易上手,而且能达到很好的体验效果,虽然说在项目 ...

  6. JLable设置复制粘贴

    final JLabel keyLable = new JLabel(key); keyLable.addMouseListener(new MouseAdapter() { @Override pu ...

  7. Python菜鸟之路:Django 数据库操作进阶F和Q操作

    Model中的F F 的操作通常的应用场景在于:公司对于每个员工,都涨500的工资.这个时候F就可以作为查询条件 from django.db.models import F models.UserI ...

  8. 查找杀死指定进程delphi

    //需要引用tlhelp32单元//查找进程function findProcessId(pname:string):Cardinal; var hsnapshot:THandle; lpe:TPro ...

  9. MySQL中InnoDB存储引擎中的哈希算法

    InnoDB存储引擎使用哈希算法来对字典进行查找,其冲突机制采用链表方式,哈希函数采用除法散列方式.对于缓冲池页的哈希表来说,在缓冲池中的Page页都有一个chain指针.它指向相同哈希函数值的页的. ...

  10. Python进阶(5)_进程与线程之协程、I/O模型

    三.协程 3.1协程概念 协程:又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存 ...