Jdk1.7 java.nio.file.Files获取读写流的方法

1、将数据设置并保存到Properties文件

String path = PropertiesTools.class.getResource(filePath).getPath();
File file = new File(path);
//获取全路径
String canonicalPath = file.getCanonicalPath();
//通过Files获取文件的输出流
OutputStream fos = Files.newOutputStream(Paths.get(canonicalPath));

//设置属性值
Properties prop = new Properties();
prop.setProperty("id", "123");
prop.setProperty("name", "张三");

//将此 Properties 表中的属性列表(键和元素对)写入输出流,将设置的属性值通过输出流保存到文件中
prop.store(fos, null);

2、获到Properties文件的属性值

String path = PropertiesTools.class.getResource(filePath).getPath();
File file = new File(path);
//获取全路径
String canonicalPath = file.getCanonicalPath();

//通过Files获取文件输入流
InputStream is = Files.newInputStream(Paths.get(canonicalPath));

Properties prop = new Properties();
//从输入流中读取属性列表
prop.load(is);

String id = prop.getProperty("id");
String name = prop.getProperty("name"); 

这里写图片描述

多看jdk api,多点进去看源码,多我修炼自已的内功心法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值