Properties类

作者 : 开心源码 本文共4231个字,预计阅读时间需要11分钟 发布时间: 2022-05-12 共207人阅读

Properties类

Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息。

往properties文件写入数据:

public class Demo3 {    public static void main(String[] args) throws IOException {        creatProperties();    }    //保存配置文件文件的信息。    public static void creatProperties() throws IOException{        //创立Properties        Properties properties = new Properties();        properties.setProperty("陈奕迅", "歌颂");        properties.setProperty("杨千嬅","再见二丁目");        properties.setProperty("谢安琪","年度之歌");        // 遍历Properties        /*Set<Entry<Object, Object>> entrys = properties.entrySet();        for(Entry<Object, Object> entry  :entrys){            System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());        }*/                //使用Properties生产配置文件。        //第一个参数是一个输出流对象,第二参数是使用一个字符串形容这个配置文件的信息。        //properties.store(new FileOutputStream("E:\\songs.properties"), "songList");         properties.store(new FileWriter("E:\\songs.properties"), "list");    }}

读取Properties文件的数据:

public class Demo3 {    public static void main(String[] args) throws IOException {        //creatProperties();        readProperties();    }        //读取配置文件的信息     public static void readProperties() throws IOException{        //创立Properties对象        Properties properties = new Properties();        //加载配置文件信息到Properties中        properties.load(new FileReader("F:\\songList.properties"));        //遍历        Set<Entry<Object, Object>> entrys = properties.entrySet();        for(Entry<Object, Object> entry  :entrys){            System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());        }        //修改陈奕迅的歌曲        //把修改后的Properties再生成一个配置文件        properties.setProperty("陈奕迅", "全世界失眠");        properties.store(new FileWriter("F:\\\\songList.properties"), "newList");    }        //保存配置文件文件的信息。    public static void creatProperties() throws IOException{        //创立Properties        Properties properties = new Properties();        properties.setProperty("陈奕迅", "歌颂");        properties.setProperty("杨千嬅","再见二丁目");        properties.setProperty("谢安琪","年度之歌");        // 遍历Properties        /*Set<Entry<Object, Object>> entrys = properties.entrySet();        for(Entry<Object, Object> entry  :entrys){            System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());        }*/                //使用Properties生产配置文件。        //第一个参数是一个输出流对象,第二参数是使用一个字符串形容这个配置文件的信息。        //properties.store(new FileOutputStream("E:\\songs.properties"), "songList");         properties.store(new FileWriter("F:\\songs.properties"), "list");    }}

Properties要注意的细节:

  1. 假如配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流处理,假如使用字节流生成配置文件的话,默认使用的是iso8859-1码表进行编码存储,这时候会出现乱码。
  2. 假如Properties中的内容发生了变化,肯定要重新使用Properties生成配置文件(store),否则配置文件信息不会发生变化。

store(OutputStream out,String comments);
store(Writer writer,String comments);
参数一:输出流对象
参数二:使用一个字符串形容这个配置文件的信息。

使用properties文件记录软件的运行次数,超过三次之后提醒购买正版,退出JVM:

public class Demo {        public static void main(String[] args) throws IOException {        File file = new File("F:\\count.properties");        if(!file.exists()){            //假如配置文件不存在,则创立该配置文件            file.createNewFile();        }                //创立Properties对象。        Properties properties = new Properties();        //把配置文件的信息加载到properties中        properties.load(new FileInputStream(file));        //此处注意创立输出流要在加载了properties之后,由于每次创立流,都会把之前的内容清空。        FileOutputStream fileOutputStream = new FileOutputStream(file);        int count = 0; //定义该变量是用于保存软件的运行次数的。        //读取配置文件的运行次数        String value = properties.getProperty("count");        if(value!=null){            count = Integer.parseInt(value);        }                //判断使用的次数能否已经达到了三次,        if(count==3){            System.out.println("您已经超出试用次数,请购买正版软件!!");            System.exit(0);        }        count++;        System.out.println("你已经使用了本软件第"+count+"次");        properties.setProperty("count",count+"");        //使用Properties生成一个配置文件        properties.store(fileOutputStream,"runtime");            }}

实际开发中的使用

Props工具类:用来读取Properties文件中的信息

import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Reader;import java.util.Properties;public class Props {        private static String configPath = "C:\\config\\config.properties";    public static String getConfigPath() {        return configPath;    }    public static void setConfigPath(String configPath) {        Props.configPath = configPath;    }        private static Properties props = new Properties();        private static void loadProperty() {        InputStream ins = null;        try {            ins = new BufferedInputStream(new FileInputStream(configPath));            props.load(ins);        } catch (IOException e) {            e.printStackTrace();        } finally {            if (ins != null) {                try {                    ins.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }        public static String getString(String key) {        if (props.get(key) == null) {            loadProperty();        }        return (String) props.get(key);    }        public static boolean getBoolean(String key) {        if (props.get(key) == null) {            loadProperty();        }        String val = (String) props.get(key);        return Boolean.parseBoolean(val);    }        public static int getInt(String key) {        if (props.get(key) == null) {            loadProperty();        }        String val = (String) props.get(key);        return Integer.parseInt(val);    }    }

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Properties类

发表回复