zoukankan      html  css  js  c++  java
  • Properties持久化键值对

    Properties
    特点:
    1.HashTable的子类,map集合中的方法都可以用。
    2.该集合没有泛型,键值都是字符串。
    3.它是一个可以持久化的属性集,键值可以存储到集合中,也可以存储到持久化设备上
    键值的来源也可以是持久化设备。

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    public class PropertyDemo {
    
        public static void main(String[] args) throws IOException {
            
            /*
             * 演示一下Properties的特有方法
             */
    //        methodDemo();
            
            methodDemo2();
        }
        
        public static void methodDemo2() throws IOException{
            
            Properties prop = new Properties();
            
            prop.setProperty("zhangsan", "20");
            prop.setProperty("lisi", "23");
            prop.setProperty("wangwu", "10");
            
            //将集合中的数据持久化到设备上
            //需要输出流对象
            FileOutputStream fos = new FileOutputStream("tempfile\info.properties");
            
            //使用prop的store方法,
            prop.store(fos, "my demo person info");//store需要用到输出流
            
            fos.close();
            
        }
        
        public static void methodDemo(){
            
            //Properties的基本存和取
            
            //1.创建一个Properties
            Properties prop = new Properties();
            
            prop.setProperty("zhangsan", "20");
            prop.setProperty("lisi", "23");
            prop.setProperty("wangwu", "10");
            
            prop.list(System.out);//此方法对调试很有用
            
    //        Set<String> set = prop.stringPropertyNames();
    //        
    //        for(String name:set){
    //            String value = prop.getProperty(name);
    //            System.out.println(name+"...."+value);
    //        }
        }
    
    }
  • 相关阅读:
    Asp.Net MVC 体验 1
    myisamchk命令进行崩溃恢复Myisam数据表
    nginx 全局变量
    centos开机启动项设置命令:chkconfig
    redis info 参数说明
    PHP中Imagick的使用
    查看当前nginx、mysql的连接数
    wget参数及用法
    编辑器与IDE
    广州求职,工作经验>5.期待伯乐
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5905689.html
Copyright © 2011-2022 走看看