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

本文详细介绍了Java中Properties类的使用,包括加载和写入properties文件的方法,如通过字节流和字符流加载,以及如何添加键值对并保存到文件。还展示了对Properties文件的读取和写入操作,以及封装工具类的实现,强调了不同写入方式的注意事项。
摘要由CSDN通过智能技术生成

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

一、Properties类API

1、Properties类描述

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

842c83c9203bfe53859865a252d72a2b.png

2、Properties方法说明

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

bd88f387a661355cd8684372eb802064.png

二、Properties类初步使用

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

File》文件流》load

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

public classTestProperties {public static void main(String[] args) throwsIOException {

File f= new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());//字节流处理

InputStream is = newFileInputStream(f);

Properties p= newProperties();

p.load(is);

System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));

is.close();//字符流处理

FileReader fr = newFileReader(f);

Properties p2= newProperties();

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的流一样才可以操作成功),这是可以组合使用的

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

File f = new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());//字节流处理

InputStream is = newFileInputStream(f);

Properties p= newProperties();

p.load(is);

System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));

is.close();

p.setProperty("key", "value");

OutputStream os= newFileOutputStream(f);

p.store(os,"2018.5.10");

View Code

文件操作结果内容:

#2018.5.10#Thu May10 00:27:29 CST 2018

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

key=value

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

再看另外的写入方式:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

File f = new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());//字节流处理

InputStream is = newFileInputStream(f);

Properties p= newProperties();

p.load(is);

System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));

is.close();

p.setProperty("key", "value");

PrintWriter os= newPrintWriter(f);

p.store(os,"2018.5.10");

View Code

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

第三种方式:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

File f = new File(new TestProperties().getClass().getResource("request-mapping.properties").getPath());//字节流处理

InputStream is = newFileInputStream(f);

Properties p= newProperties();

p.load(is);

System.out.println(p.getProperty("/nutrition/food/foodClassificationController"));

is.close();

p.setProperty("key", "value");

PrintWriter os= newPrintWriter(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文件的读操作

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecn.nutrition.common.util;importjava.io.Closeable;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.HashMap;importjava.util.Map;importjava.util.Map.Entry;importjava.util.Properties;importjava.util.Set;public classPorpertiesUitl {public staticProperties getProperties(String filePath)

{

Properties properties= newProperties();

File f= new File(PorpertiesUitl.class.getResource("request-mapping.properties").getPath());

InputStream is= null;try{

is= newFileInputStream(f);

properties.load(is);if(null !=is) {

is.close();

}

}catch(IOException e) {

e.printStackTrace();

}finally{

closeIO(is);

}returnproperties;

}public static MapgetMapping(String filePath)

{

Map map = new HashMap();

Properties properties=getProperties(filePath);

Set> entrys =properties.entrySet();for (Entryentry : entrys) {

map.put(entry.getKey().toString(), entry.getValue().toString());

}returnmap;

}private static voidcloseIO(Closeable io)

{if(null !=io) {try{

io.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

View Code

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

xml文件的加载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值