zoukankan      html  css  js  c++  java
  • Properties加载设备上的数据

    Properties的两个方法:

    store(OutputStream out, String comments):持久化

    load(InputStream inStream):加载

    load(Reader reader)

    store(Writer writer, String comments)

    import java.io.FileInputStream;
    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的特有方法
             */
            
            methodDemo3();
        }
        
        public static void methodDemo3() throws IOException {
            
            Properties prop = new Properties();
            
            //定义读取流和数据文件关联
            FileInputStream fis = new FileInputStream("tempfile\info.properties");
            prop.load(fis);
            
            prop.setProperty("zhangsan", "12");
            
            //要将改完的数据重新持久化
            
            FileOutputStream fos = new FileOutputStream("tempfile\info.properties");
            prop.store(fos, "flush data");//第二个参数是注释的意思
            
            
            prop.list(System.out);
            
            fos.close();
            fis.close();
            
            
        }
    
        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);
    //        }
        }
    
    }
  • 相关阅读:
    搭建woocomerce网站
    Cozmo 机器人编程环境搭建
    DevExpress Wizard的控件使用方法
    DevExpress 地图的控件使用方法
    DevExpress 摄像机的控件使用方法
    大疆第一人称视角眼镜goggle激活
    iis支持asp.net4.0的注册命令使用方法
    【转】PowerDesigner删除外键关系,而不删除外键列
    【转】ABP webapi三种方式
    【转】OAuth2.0的refresh token
  • 原文地址:https://www.cnblogs.com/qjlbky/p/5905707.html
Copyright © 2011-2022 走看看