【001】java中配置文件properties的操作

  properties文件在java的使用是比较常见的用于配置key-value的配置文件,java中也有专门对该种类型的文件进行处理的类Properties

 一、Properties类API

1、Properties类描述

  Properties继承了HashTable,明确的表示了它是一个key-value的集合,类中描述了可以使用get、put方法但是要保证key-value都是字符串否则会在写入文件时出错,也可以用其本身的getProperty、setProperty来操作这是安全的。

2、Properties方法说明

  方法的描述相对比较容易理解,主要包含load输入流(将properties的key-value解析出来)、键值对集合操作、将集合内容写入到输出流(store、list)

二、Properties类初步使用

1、要将properties的文件内容读取到Properties集合里面首先要load,这里提供了三种方法(字符流、字节流、以及xml的load),那么过程是这样的:

File》文件流》load

public class TestProperties {

    public static void main(String[] args) throws IOException {
        File f = new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());
        //字节流处理
        InputStream is = new FileInputStream(f);
        Properties p = new Properties();
        p.load(is);
        System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));
        is.close();
        //字符流处理
        FileReader fr = new FileReader(f); 
        Properties p2 = new Properties();
        p2.load(fr);
        System.out.println(p2.getProperty("/nutrition/food/foodClassificationController"));
        fr.close();
    }
}
View Code

request-mapping.properties文件内容如下

/nutrition/food/foodClassificationController = cn.nutrition.food.controller.FoodClassificationController

结果两种方式都打印了cn.nutrition.food.controller.FoodClassificationController的值

2、properties文件内容的写也有几种方式list对应的两种文件流(没有限定load的流类型)、store对应的两种文件流(必须跟load的流一样才可以操作成功),这是可以组合使用的

        File f = new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());
        //字节流处理
        InputStream is = new FileInputStream(f);
        Properties p = new Properties();
        p.load(is);
        System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));
        is.close();
        p.setProperty("key", "value");
        OutputStream os = new FileOutputStream(f);
        p.store(os,"2018.5.10");
View Code

文件操作结果内容:

#2018.5.10
#Thu May 10 00:27:29 CST 2018
/nutrition/food/foodClassificationController=cn.nutrition.food.controller.FoodClassificationController
key=value

这里除了键值对,还有两行“#”标记的注释内容

再看另外的写入方式:

        File f = new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());
        //字节流处理
        InputStream is = new FileInputStream(f);
        Properties p = new Properties();
        p.load(is);
        System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));
        is.close();
        p.setProperty("key", "value");
        PrintWriter os = new PrintWriter(f);
        p.store(os,"2018.5.10");
View Code

文件操作结果跟上面的一样,貌似跟api里面的描述有差异,具体的原因暂且先放一放

第三种方式:

        File f = new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());
        //字节流处理
        InputStream is = new FileInputStream(f);
        Properties p = new Properties();
        p.load(is);
        System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));
        is.close();
        p.setProperty("key", "value");
        PrintWriter os = new PrintWriter(f);
        p.list(os);
        os.flush();os.close();
View Code

注意,这里os需要调用flush。

进行多次以上的操作结果:

-- listing properties --
--=listing properties --
/nutrition/food/foodClassificationController=cn.nutrition.food.controller.FoodClas...
key=value

这证明,这种方式只合适用作日志打印list(System.out),不可以使用此种方式写文件,否则容易造成数据丢失。

三、properties文件操作的工具封装,这里封装只针对于properties文件的读操作

package cn.nutrition.common.util;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class PorpertiesUitl {
    
    public static Properties getProperties(String filePath)
    {
        Properties properties = new Properties();
        File f = new File(PorpertiesUitl.class.getResource("request-mapping.properties").getPath());
        InputStream is = null;
        try {
            is = new FileInputStream(f);
            properties.load(is);
            if(null != is) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeIO(is);
        }
        return properties;
    }
    
    public static Map<String, String> getMapping(String filePath)
    {
        Map<String, String> map = new HashMap<String, String>();
        Properties properties = getProperties(filePath);
        Set<Entry<Object, Object>> entrys = properties.entrySet();
        for (Entry<Object, Object> entry : entrys) {
            map.put(entry.getKey().toString(), entry.getValue().toString());
        }
        return map;
    }
    
    private static void closeIO(Closeable io)
    {
        if(null != io) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
View Code

 

这里涉及到source目录下的文件加载路径问题,参考

xml文件的加载

 

posted @ 2018-05-10 20:44  武汉-石头  阅读(521)  评论(0编辑  收藏  举报