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();
        }
        
        
    }
}
原文地址:https://www.cnblogs.com/wwzyy/p/5549483.html