Properties

Properties

Properties继承于HashTable,来表示一个持久的属性集,它使用键值结构存储数据,每个键以及其对应值都是一个字符串,该类也被许多Java类使用,比如获取系统属性,System.getProperties方法就是放回一个Properties对象。Properties可保存在流中或从流中加载。

Properties集合是唯一和IO流相结合的集合

Properties类

构造方法

public Properties()://创建一个空的属性列表
    //基本的存储方法
public Object setProperty(String key, String value)://保存一对属性
public String getProperty(String key)://使用此属性列表中的指定的键搜索属性值
public Set<String> stringPropertyNames()://所有键的名称的集合

可以使用Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中储存

void store(OutputStream out, String comments)//字节输出流,不能写入中文  
void store(Writer writer, String comments)  //字符输出流可以写中文
    //参数comments:注释,用来解释说明保存的文件是做什么的不能使用中文,会产生乱码,默认是Unicode编码 一般使用""空字符串

可以使用Properties集合中的方法Load,把硬盘保存的文件(键值对),读取到集合中使用。

void load(InputStream inStream) //从输入字节流读取属性列表(键和元素对)。 不可以读取中文
void load(Reader reader)  ///从输入字符流读取属性列表(键和元素对)可以读取中文

注意

1.存储键值对的文件中,键与值默认的连接符号可以使用=,空格(其他符号)

2.存储键值对的文件中,可以使用#进行注释,被注释的键值对不会在被读取

3.存储键值对的文件中,键与值默认都是字符串,不需要再加引号

Properties集合是一个双列集合,key和value默认都是字符串。

例子

public class Demo01Properties {
    public static void main(String[] args) throws IOException {
        File file=new  File("C:\\Users\\Administrator\\Desktop\\Study\\src\\com\\bainan\\io\\c.txt");
        show01(file);
        show02(file);
        show03(file);
    }
//使用load把硬盘中保存的文件(键值对),读取到集合中使用
    private static void show03(File file) {
        Properties prop=new Properties();
        FileReader fr = null;
        try {
            fr = new FileReader(file);
            prop.load(fr);
            Set<String> set = prop.stringPropertyNames();
            Iterator<String> iterator=set.iterator();
            while (iterator.hasNext()){
                String next = iterator.next();
                System.out.println(next+"="+prop.getProperty(next));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //Properties集合中的方法store,把集合中的临时数据,持久化写入到硬盘中储存
    private static void show02(File file) {
        Properties prop=new Properties();
        prop.setProperty("1","a");
        prop.setProperty("2","b");
        prop.setProperty("3","c");
        try(FileOutputStream fos=new FileOutputStream(file,true)){
            prop.store(fos,"save prop");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
//使用Properties集合存储数据,遍历取出Properties集合中的数据

    private static void show01(File file) throws IOException {
        Properties prop=new Properties();
        prop.setProperty("赵丽颖","168");
        prop.setProperty("迪丽热巴","165");
        prop.setProperty("古力娜扎","160");
        Set<String> set = prop.stringPropertyNames();
        for (String key:set) {
            System.out.println(key+"="+prop.getProperty(key));
        }
        FileWriter fw=new FileWriter(file);
        prop.store(fw,"1");
        fw.close();

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值