zoukankan      html  css  js  c++  java
  • Properties类读取配置文件

    package com.wzy.t4;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Properties;
    
    
    public class PropertityTest {
        public static void main(String[] args) {
            
            Properties file = new Properties();
            file.setProperty("name", "小明");
            file.setProperty("passwd", "123456");
            file.setProperty("driver", "com.mysql.jdbc.Driver");
            file.setProperty("host", "127.0.0.1");
    //        System.out.println(file.getProperty("name", "defaultValue"));
    //        System.out.println(file.getProperty("name1", "defaultValue"));
            
            try {
                //将数据保存到本地磁盘,后缀一般为(.properties)以表明是Properties使用的
                file.store(new FileOutputStream(new File("d:"+File.separator+"profile.properties")), "注释内容");
                //从本地磁盘读取文件或从类路径下读取数据
                file.load(new FileInputStream(new File("d:"+File.separator+"profile.properties")));
                System.out.println(file.getProperty("host", ""));
                
                /**
                    #u6CE8u91CAu5185u5BB9
                    #Wed Jun 01 14:16:14 CST 2016
                    passwd=123456
                    name=u5C0Fu660E
                    host=127.0.0.1
                    driver=com.mysql.jdbc.Driver
                 * */
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
        }
    }
  • 相关阅读:
    MySQL (下篇)
    【JUC剖析】专栏总集篇
    CF1391D(思维)
    CF1393E2(字符串)
    洛谷P5405 [CTS2019]氪金手游(期望,容斥)
    P5293 [HNOI2019]白兔之舞(单位根反演)
    洛谷P5400 [CTS2019]随机立方体(计数)
    洛谷P5401 [CTS2019]珍珠(生成函数)
    支配树学习笔记
    UOJ455 雪灾与外卖(模拟费用流)
  • 原文地址:https://www.cnblogs.com/wwzyy/p/5549483.html
Copyright © 2011-2022 走看看