zoukankan      html  css  js  c++  java
  • java基础50 配置文件类(Properties)

    1、 配置文件类Properties的概念

        主要生产配置文件与读取配置文件的信息

    2、Properties要注意的细节

        1.如果配置文件一旦使用了中文,那么在使用store方法生产的配置文件额时候字符流解决,如果使用字节流生产的配置文件的话,默认使用的编码是iso8895-1码表经行编码存储,这个时候会出现乱码.
        2.如果Properties中内容发生了改变,一定要重新使用Properties生成配置文件,否则配置文件不会发生改变.

    3、实例

     1 package com.dhb.file;
     2 
     3 import java.io.FileNotFoundException;
     4 import java.io.FileReader;
     5 import java.io.FileWriter;
     6 import java.io.IOException;
     7 import java.util.Map.Entry;
     8 import java.util.Properties;
     9 import java.util.Set;
    10 
    11 /**
    12  * @author DSHORE / 2018-7-18
    13  *
    14  */
    15 public class Demo24 {
    16     public static void main(String[] args) throws Exception {
    17         //createProperties();
    18         readProperties();
    19     }
    20     //读取配置文件的信息
    21     public static void readProperties() throws Exception, IOException{
    22         //创建Properties对象
    23         Properties properties=new Properties();
    24         //加载配置文件信息到Properties里面
    25         properties.load(new FileReader("F:\person.properties"));
    26         //遍历
    27         /*Set<Entry<Object,Object>> entrys=properties.entrySet();
    28         for (Entry<Object, Object> entry : entrys) {
    29             System.out.println("键:"+entry.getKey()+",值:"+entry.getValue());
    30             //修改密码
    31             //把修改后的properties在生成一个配置文件
    32             properties.setProperty("李四","001");
    33             properties.store(new FileWriter("F:\person.properties"), "This is QQ account and password properties");
    34         }*/
    35         //修改密码
    36         //把修改后的properties在生成一个配置文件
    37         properties.setProperty("李四","007");
    38         properties.store(new FileWriter("F:\person.properties"), "This is QQ account and password properties");
    39     }
    40     //创建配置文件
    41     public static void createProperties() throws FileNotFoundException, IOException{
    42         //创建一个配置文件
    43         Properties properties=new Properties();
    44         properties.setProperty("张三", "123");
    45         properties.setProperty("李四", "234");
    46         properties.setProperty("王五","345");
    47         //遍历Properties
    48         /*Set<Entry<Object,Object>> entrys=properties.entrySet();
    49         for (Entry<Object, Object> entry : entrys) {
    50             System.out.println("键:"+entry.getKey()+",值:"+entry.getValue());
    51         }*/
    52         
    53         //使用Properties生产配置文件.
    54         //properties.store(new FileOutputStream("F:\person.properties"), "hehe");//第一个参数是一个输出流对象,第二参数是描述这个配置文件的信息
    55         properties.store(new FileWriter("F:\person.properties"), "This is QQ account and password properties");
    56     }
    57 }

    运行结果图

    原创作者:DSHORE

    作者主页:http://www.cnblogs.com/dshore123/

    原文出自:https://www.cnblogs.com/dshore123/p/9328488.html

    欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

  • 相关阅读:
    我的周记17——“世界再美,也美不过我一日三餐,心情再坏,也坏不过我没心没肺”
    【测试-移动端】 聊聊手机端的测试
    Java基础篇---多线程
    BIO和NIO实现文件复制
    IO操作-BIO
    js数组去重
    js json对象操作
    23种设计模式,每天一种设计模式(2)
    23种设计模式,每天一种设计模式
    .net画二叉树
  • 原文地址:https://www.cnblogs.com/dshore123/p/9328488.html
Copyright © 2011-2022 走看看