Properties

@(笔记)[MarkDown|集合|我的博客|每日一类]
07.21

Properties类

public class Propertiesextends Hashtable<Object,Object>
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
新建一个文件
内容为
键1=值1
键2=值2

使用此类可以对文件数据进行持久化保存

方法的使用

  • load(InputStream inStream); 从输入流中读取属性列表(键和元素对)。
Properties pro = new Properties();
pro.load(new FileInputStream(相对路径或绝对路径));//此方法将抛出FileNotFoundException
  • getPropertiy(String key) 使用指定的键在此属性列表中搜索属性,返回String
  • getProperty(String key, String defaultValue) 用指定的键在属性列表中搜索属性。返回String
  • setProperty(String key, String value) 如果存在了这个键,则更改对应的值,如果不存在,则添加这个属性,这个方法是调用Hashtable的put的结果
  • public void store(OutputStream out, String comments) throws IOException写入文件中
props.store(new FileOutputStream(相对路径或绝对路径), 说明);

Properties 综合使用

/*
* @Author: Code.Ai
* @Date:   2015-07-21 23:57:11
* @Last Modified by:   Code.Ai
* @Last Modified time: 2015-07-22 00:51:58
*/

import java.io.*;
import java.util.*;
public class PropertiesTest {
    static Properties props = new Properties();
    public static void main(String[] args) {
        //读文件
        Read();
        System.out.println(props.getProperty("Teacher"));
        //添加数据
        //使用setProperties()方法,如果文件中没有相同的键,则添加,如果有相同的,就覆盖
        props.setProperty("Teacher","胡老大");
        props.setProperty("Student","Code");
        System.out.println(props.getProperty("Student"));
        Save();
        //删除全部数据
        //直接在没有加载文件的情况下使用store()方法存入空数据到文件中
        Read();
        //存入Sudtent对象
        for(int i = 0 ; i < 3; i++){
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入学生名字:");
            String name = scan.next();
            System.out.println("请输入学生年龄:");
            int age = scan.nextInt();
            System.out.println("请输入学生成绩");
            int score = scan.nextInt();
            Student stu = new Student(name,age,score);
            props.setProperty(stu.getName(), stu.getName() + "&" + stu.getAge() + "&" + stu.getScore());
        }
        Save();
        //删除指定的键  直接调用Map类的remove方法
        props.remove("Student");
        Save();
        //构造学生对象
        //把值取出来分解,然后赋值给学生
        //调用继承Map的values()方法,取得所有的值
        Collection<String> cl = props.values();
        for(String str : cl){
            System.out.println(str);
            //拆分字符串
        }
    }
    public static void Read(){
        try {
            props.load(new FileInputStream("data.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //存文件
    public static void Save(){
        try {
            props.store(new FileOutputStream("data.properties"), null);
        } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
        // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值