Properties

来源:互联网 发布:mac qq接受文件没反应 编辑:程序博客网 时间:2024/06/15 23:46

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

  • Properties类
    • 方法的使用
    • Properties 综合使用

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
原创粉丝点击