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();
            }
            
            
        }
    }
  • 相关阅读:
    [转]<UNIX编程艺术>之精华文字
    2012年学习小结
    重试逻辑代码
    《构建高性能web站点》读书点滴
    mysql体系结构
    设计memcache的部署结构
    一般性hash算法c#版
    Flex带进度条的多文件上传(基于Servlet)
    C++11中值得关注的几大变化
    优化网站响应时间tomcat添加gzip
  • 原文地址:https://www.cnblogs.com/wwzyy/p/5549483.html
Copyright © 2011-2022 走看看