java对properties配置文件的读写操作

1.1.  对properties配置文件的读取

1.1.1.  要读取的文件

配置文件key和value之间用冒号[:]和等于号[=]都是可以的.


Test.properties


name:\u5F20\u4E09

password:\u5BC6\u7801

age:22

1.1.2.  读取的程序代码


PropertiesTest.java

/**
     * 读取配置文件
     * @throws IOException
     */
    @Test
    public void read() throws IOException{
       Properties properties = new Properties();

       File file = new File("test.properties");

       FileInputStream fis = new FileInputStream(file);

       properties.load(fis);

       Set<Object> keySet = properties.keySet();

       for (Object object : keySet) {
           System.out.println(object.toString()+":"+properties.getProperty(object.toString()));
       }
    }

1.1.3.  结果


后台打印结果


age:22

password:密码

name:张三

1.2.  对propertiest配置文件的写入操作

1.2.1.  写入程序的代码


PropertiesTest.java

/**
     * 向配置文件写入内容
     * @throws IOException
     */
    @Test
    public void write() throws IOException {
       Properties properties = new Properties();

       String file = "aa.properties";
       FileOutputStream fos = new FileOutputStream(file);

       properties.setProperty("aa", "123");
       properties.setProperty("bb", "456");
       properties.setProperty("cc", "789");
       properties.setProperty("name", "张三");

       properties.store(fos, "保存文件");

       fos.close();
    }

1.2.2.  结果


aa.propertiest


#\u4FDD\u5B58\u6587\u4EF6

#Sun Jan 25 14:06:25 CST 2015

name=\u5F20\u4E09

aa=123

bb=456

cc=789

1.3.  对properties配置文件的修改操作

1.3.1.  修改前的配置文件


Test.properties


Age=22

Password=密码

Name=张三

1.3.2.  修改程序的代码


PropertiesTest.java

  /**
     * 对配置文件的修改
     * @throws IOException
     */
    @Test
    public void update() throws IOException{

       Properties properties = new Properties();

       File file = new File("test.properties");

       FileInputStream fis = new FileInputStream(file);

       FileOutputStream fos = new FileOutputStream(file);

       properties.load(fis);

       properties.setProperty("name", "李四");
       properties.setProperty("age", "222");
       properties.setProperty("age3", "222");
       properties.store(fos, "更新配置文件");

       fos.close();

       fis.close();

    }

1.3.3.  修改后的文件内容


Test.properties


#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6

#Sun Jan 25 15:20:40 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

age3=222

1.4.  对properties配置文件的删除

1.4.1.  删除前配置文件的内容


Test.properties


#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6

#Sun Jan 25 15:20:40 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

age3=222

1.4.2.  删除程序的代码


PropertiesTest.java

/**
     * 删除配置文件中的key
     * @throws IOException
     *
     */
    @Test
    public void delete() throws IOException{
       Properties properties = new Properties();

       File file = new File("test.properties");

       FileInputStream fis = new FileInputStream(file);

       properties.load(fis);   

       //必须先用map将所有的内容先保存,不然一删除,原来的内容都没了
       Map<String, String> map = new HashMap<String, String>();

       Set<Object> keySet = properties.keySet();

       System.out.println(keySet.size());

       for (Object object : keySet) {
           String key = (String) object;
           String value = (String) properties.get(key);
           System.out.println(key+"="+value);
           map.put(key, value);
       }
       //删除key
       map.remove("age3");
       properties.remove("age3");

       for (java.util.Map.Entry<String, String> entry: map.entrySet()) {
           properties.setProperty(entry.getKey(), entry.getValue());
       }

       FileOutputStream fos = new FileOutputStream(file);
       properties.store(fos, "删除配置文件中的key:age3");     

       fos.close();

       fis.close();

    }  

1.4.3.  删除后的配置文件内容


Test.properties


#\u5220\u9664\u914D\u7F6E\u6587\u4EF6\u4E2D\u7684key:age3

#Sun Jan 25 15:23:58 CST 2015

age=222

name=\u674E\u56DB

password=\u5BC6\u7801

1.5.  完整程序代码


PropertieTest.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.junit.Test;

public class PropertiesTest {

    public static void main(String[] args) {

    }
    /**
     * 删除配置文件中的key
     * @throws IOException
     *
     */
    @Test
    public void delete() throws IOException{
       Properties properties = new Properties();

       File file = new File("test.properties");

       FileInputStream fis = new FileInputStream(file);

       properties.load(fis);   

       //必须先用map将所有的内容先保存,不然一删除,原来的内容都没了
       Map<String, String> map = new HashMap<String, String>();

       Set<Object> keySet = properties.keySet();

       System.out.println(keySet.size());

       for (Object object : keySet) {
           String key = (String) object;
           String value = (String) properties.get(key);
           System.out.println(key+"="+value);
           map.put(key, value);
       }
       //删除key
       map.remove("age3");
       properties.remove("age3");

       for (java.util.Map.Entry<String, String> entry: map.entrySet()) {
           properties.setProperty(entry.getKey(), entry.getValue());
       }

       FileOutputStream fos = new FileOutputStream(file);
       properties.store(fos, "删除配置文件中的key:age3");     

       fos.close();

       fis.close();

    }

    /**
     * 对配置文件的修改
     * @throws IOException
     */
    @Test
    public void update() throws IOException{

       Properties properties = new Properties();

       File file = new File("test.properties");

       FileInputStream fis = new FileInputStream(file);

       properties.load(fis);

       //必须先用map将所有的内容先保存,不然一更新,原来的内容都没了
       Map<String, String> map = new HashMap<String, String>();

       Set<Object> keySet = properties.keySet();

       System.out.println(keySet.size());

       for (Object object : keySet) {
           String key = (String) object;
           String value = (String) properties.get(key);
           System.out.println(key+"="+value);
           map.put(key, value);
       }

       map.put("name", "李四");
       map.put("age", "222");
       map.put("age3", "222");

       for (java.util.Map.Entry<String, String> entry: map.entrySet()) {
           properties.setProperty(entry.getKey(), entry.getValue());
       }
       FileOutputStream fos = new FileOutputStream(file);

       properties.store(fos, "更新配置文件");

       fos.close();

       fis.close();

    }

    /**
     * 向配置文件写入内容
     * @throws IOException
     */
    @Test
    public void write() throws IOException {
       Properties properties = new Properties();

       String file = "aa.properties";
       FileOutputStream fos = new FileOutputStream(file);

       properties.setProperty("aa", "123");
       properties.setProperty("bb", "456");
        properties.setProperty("cc", "789");
       properties.setProperty("name", "张三");

       properties.store(fos, "保存文件");

       fos.close();
    }

    /**
     * 读取配置文件
     * @throws IOException
     */
    @Test
    public void read() throws IOException{
       Properties properties = new Properties();

       File file = new File("test.properties");

       FileInputStream fis = new FileInputStream(file);

       properties.load(fis);

       Set<Object> keySet = properties.keySet();

       for (Object object : keySet) {
           System.out.println(object.toString()+"="+properties.getProperty(object.toString()));
       }
    }

}
时间: 2024-08-07 03:52:07

java对properties配置文件的读写操作的相关文章

Java读取properties配置文件常用方法

在开发中对properties文件的操作还是蛮经常的,所以总结了几种操作方法,为后面的开发可以进行参考. 1.通过java.util.ResourceBundle类来读取 这边测试用到了枚举类进行传入文件的key值,然后获取value,可以进行灵活的配置. 通过这种方式读取properties文件不需要加.properties后缀名,只需文件名即可,如果有放在某一个包下,要加包的限定名,如放在com.frame.util包下,则要路径要用com/fram/util config.properti

java读取properties配置文件总结

java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1).使用getResourceAsStream()方法读取配置文件. (2).使用InputStream()流去读取配置文件. 注意:在使用getResourceAsStream()读取配置文件时,要特别注意配置文件的路径的写法. this.getClass.getResourceAsStream(f

java读取properties配置文件

java读取.properties配置文件 这两天做java项目,用到属性文件,到网上查资料,好半天也没有找到一个满意的方法能让我读取到.properties文件中属性值,很是郁闷,网上讲的获取属性值大概有以下方法,以下三种方法逐渐优化,以达到最好的效果以下都以date.properties文件为例,该文件放在src目录下,文件内容为 startdate=2011-02-07 totalweek=25 方法一: public class Stweek { static private Strin

java对.properties配置文件操作

实现运用Java.util.Properties来进行对.properties配置文件操作. 配置文件实例:如debug.properties #Tue Mar 21 15:46:17 CST 2017 #key=value remote.debug.prot=7451 第一步写个获取文件路径的外部方法 //-in- String filePath:配置文件名如debug.properties-- //-return- 文件类对象-- public static File getFile (St

java读取.properties配置文件的几种方法

读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util.HashTable,即实现了Map接口,所以,可使用相应的方法来操作属性文件,但不建议使用像put.putAll这两个方法,因为put方法不仅允许存入String类型的value,还可以存入Object类型的.因此java.util.Properties类提供了getProperty()和setPr

Java 读取Properties配置文件

读取Properties配置文件的方法,经常忘记,记录下来备忘一下: package utils; import java.io.IOException;import java.io.InputStream; import java.util.Properties; public class PropertyConfig { private static Properties config; static { config = new Properties(); InputStream in =

Java读取properties配置文件时,中文乱码解决方法

碰到了用java.util.Properties读取中文内容(UTF-8格式)的配置文件,发生中文乱码的现象 Properties prop=new Properties(); prop.load(Client.class.getClassLoader().getResourceAsStream("config.properties")); 由于使用这样的加载方式使用了系统默认的编码格式,不是UTF-8格式的读取模式,就会发生乱码情况. 正确解决方法 Properties prop=n

Java 读取properties 配置文件的几种方式

基于ClassLoder读取配置文件 Properties properties = new Properties(); // 使用ClassLoader加载properties配置文件生成对应的输入流 InputStream in = PropertiesMain.class.getClassLoader().getResourceAsStream("config/config.properties"); // 使用properties对象加载输入流 properties.load(

Java读取.properties配置文件

1.源码 /** * 读取配置文件[目前只测了.properties配置文件]的工具类 * Created by li on 2015/9/13. */ public class PropertiesUtils { private static PropertiesConfiguration propertiesConfiguration; static { try { //初始化实例 propertiesConfiguration = new PropertiesConfiguration("