java properties 新建_java入门 -- 使用Properties创建和读取配置文件

/* * Properties(配置文件类)

* 作用:用于生成配置文件与读取配置文件的信息;基于Hash表来存储的,是无序的;

* 注意事项:

* 1. 如果配置文件出现了中文字符,只能使用字符流来生成配置文件,因为使用字节流生成配置文件的话,使用的是ISO8859-1编码,会出现乱码;

* 2. 如果properties中的内容发生了变化,那么需要重新存储配置文件;

*/

package com.michael.lin;

import java.io.FileNotFoundException;

mport java.io.FileOutputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Properties;

import java.util.Set;

public class Demo04 {

public static void main(String[] args) throws FileNotFoundException, IOException{

readProperties();

readProperties1();

}

//创建配置文件

public static void readProperties() throws FileNotFoundException, IOException{

//创建Properties

Properties properties = new Properties();

//添加属性

properties.setProperty("jinge", "123");

properties.setProperty("michael", "xxx1");

properties.setProperty("kinggle", "3457");

//遍历

PropertiesSet> entrys = properties.entrySet();

for(Entryentry : entrys){

System.out.println(entry.getKey() + ":" + entry.getValue());}

//使用properties产生配置文件  store方法是一个文件对象,第二个参数是描述内容;

//properties.store(new FileOutputStream("C:\\conf.properties"), "配置文件");

properties.store(new FileWriter("c:\\per.properties"), "配置文件");}

//读取配置文件信息

public static void readProperties1() throws FileNotFoundException, IOException{

//创建Properties对象

Properties properties = new Properties();

//加载配置文件信息

properties.load(new FileReader("c:\\conf.properties"));

Set> entrys = properties.entrySet();

for(Entryentry : entrys){

System.out.println("键" + entry.getKey() + "值" + entry.getValue());

}

//修改配置文件

properties.setProperty("jinge", "ginkk007");

//修改配置后,重新生产一个配置文件

properties.store(new FileWriter("c:\\conf.properties"), "你好吗");

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值