Java Properties

      Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置文件中很多变量是经常改变的,这样做也是为了方便用户,让用户能够脱离程序本身去修改相关的变量设置。

package com.demo.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * Properties继承自Hashtable<Object,Object>
 * 只要符合properties文件的规范即可,后缀不是强制性的要求
 * 文本注释信息可以用"#"来注释
 */
public class PropertiesDemo {

    public static void main(String[] args) {
//      String path = "file:///D:/workspace/TestApp/project.properties";
        String path = "file:///D:/workspace/TestJava/.settings/org.eclipse.jdt.core.prefs";
        String storePath1 = "D:/properties_no_comment.txt";
        String storePath2 = "D:/properties_with_comment.txt";
        String storePath3 = "D:/xml_no_comment.txt";
        String storePath4 = "D:/xml_with_comment.txt";
        try {
            URL url = new URL(path);
            Properties properties = new Properties();
            //从文件中加载properties
            properties.load(url.openStream());
            Set<Entry<Object, Object>> propertiesSet = properties.entrySet();
            Iterator<Entry<Object, Object>> iterator = propertiesSet.iterator();
            while(iterator.hasNext()) {
                Entry<Object, Object> entry = iterator.next();
                System.out.println(entry.getKey() + " = " + entry.getValue());
            }
            System.out.println();

            System.out.println("properties.getProperty(\"eclipse.preferences.version\") = " + properties.getProperty("eclipse.preferences.version"));
            //在输出的时候,key中的空格会自动添加转义字符'\'作为前缀,而value中的空格会原样输出
            properties.setProperty("My Property Key", "property value");
            System.out.println();

            String comment = "my comment";
            FileOutputStream fos = new  FileOutputStream(storePath1);
            //将properties存储到文件中
            properties.store(fos, "");
            fos.close();

            fos = new  FileOutputStream(storePath2);
            properties.store(fos, comment);
            fos.close();

            fos = new  FileOutputStream(storePath3);
            properties.storeToXML(fos, "");
            fos.close();

            fos = new  FileOutputStream(storePath4);
            properties.storeToXML(fos, comment);
            fos.close();

            Enumeration<?> nameEnum = properties.propertyNames();
            while(nameEnum.hasMoreElements()) {
                System.out.println("propertyNames = " + nameEnum.nextElement());
            }
            System.out.println();

            Enumeration<Object> valueEnum = properties.elements();
            while(valueEnum.hasMoreElements()) {
                System.out.println("elements = " + valueEnum.nextElement());
            }

            //清除所有装载的 键 - 值对(eclipse中凡是在方法的右下角有带一个时钟标志的,都表示该方法由其基类提供)
            properties.clear();
            System.out.println();

            //Java虚拟机(JVM)有自己的系统配置文件(system.properties),可以通过如下方式获取
            properties = System.getProperties();
            System.out.println(properties.toString());
            System.out.println();

            //E表示星期,k表示时区,MM数字月份,MMM月份3字节简写,MMMM月份英文3字节简写,z表示时区,a表示上午还是下午,Z表示具体时区(东八区:+0800)
            String datePattern = "E MMM dd HH:mm:ss z yyyy a k Z";
            SimpleDateFormat sdf = new SimpleDateFormat(datePattern, Locale.ENGLISH);
            System.out.println(sdf.format(new Date()));
            //默认的toString()就是这种格式:"EEE MMM dd HH:mm:ss zzz yyyy";
            System.out.println(new Date().toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这里写图片描述

“#”表示行注释
(1)如果生成properties文件,默认头2行为注释,第一行为comment,如果没有comment,就在”#”之后留空;第二行”#”记录properties文件的修改时间,格式为”EEE MMM dd HH:mm:ss zzz yyyy”,即Date.toString()。
(2)如果生成xml文件,如果没有comment,就使用闭合的comment标签。如果有comment就在comment标签组中添加上内容。注意:xml格式不会添加上修改时间。
这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值