Java_Properties

Java_Properties类

Hashtable与HashMap区别
  1. 主要:Hashtable线程安全,同步,效率相对低下

    HashMap线程不安全,异步,效率高

  1. 父类:Hashtable父类是Dictionary

    HashMap父类是AbstractMap

  1. Hashtable无论键值都不能为null

    HashMap键可以有一个为空

                    值可以有很多为null

 

    Properties继承于Hashtable

 

Properties类常用方法:
String     getProperty(String key) 
          用指定的键在此属性列表中搜索属性。

String  getProperty(String key, String defaultValue) 
          用指定的键在属性列表中搜索属性.

Object setProperty(String key, String value) 
          调用 Hashtable 的方法 put。

void store(OutputStream out, String comments) 
          以适合使用 load(InputStream) 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。

void storeToXML(OutputStream os, String comment) 
          发出一个表示此表中包含的所有属性的 XML 文档。默认编码UTF-8

void storeToXML(OutputStream os, String comment, String encoding) 
          使用指定的编码发出一个表示此表中包含的所有属性的 XML 文档。

Set<String>  stringPropertyNames() 
          返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。

Enumeration<?>  propertyNames() 
          返回属性列表中所有键的枚举,如果在主属性列表中未找到同名的键,则包括默认属性列表中不同的键。

 

四种加载资源文件方法

直接加载

    1.相对路径   

    //相对路径
      p.load(new FileInputStream(new File("db.properties")));

 

    2.绝对路径

   //绝对路径
     p.load(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\db.properties")));

 

使用类的相对路径获取资源文件

    3.类路径加载资源文件1  

    //类路径加载资源文件
      p.load(Demo3.class.getResourceAsStream("/com/ahd/Hashtable/db.properties"));;

 

    4.类路径加载资源文件2  

    p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/ahd/Hashtable/db.properties"));;

 

 练习代码
  
package com.ahd.Hashtable;

import java.util.Properties;

/***
 * Hashtable与HashMap区别:
 *         1.Hashtable线程安全,同步,效率低下
 *             HashMap线程不安全,异步,效率高
 *         2.继承的父类不同,Hashtable继承于Dictionary
 *             HashMap继承于AbstractMap
 *         3.Hashtable无论键值不能为null
 *             HashMap键可以有一个为null,值可以有很多null
 * properties是Hashtable的子类
 * @author Administrator
 *
 */
public class Demo1 {
    public static void main(String[] args) {
        Properties p=new Properties();
        //设置存储值
        p.setProperty("user", "scott");
        p.setProperty("password", "123456");
        //读取值
        String user=p.getProperty("user");
        String password=p.getProperty("password", "test");
        
        System.out.println(user);
        System.out.println(password);
        
    }
}
Demo1

 

 

  
package com.ahd.Hashtable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Demo2 {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        //设置键值
        Properties p=new Properties();
        p.setProperty("user", "scott");
        p.setProperty("password", "123456");
        
        //存入文件 .properties 绝对路径
        p.store(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\db.properties")), "db配置备注");
        p.storeToXML(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\db.xml")), "db配置备注");
        
        //存入文件,相对路径 默认是本工程
        p.store(new FileOutputStream(new File("db.properties")), "db配置备注");
        p.storeToXML(new FileOutputStream(new File("db.xml")), "db配置备注");
        
    }
}
Demo2

 

 

  
package com.ahd.Hashtable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/***
 * 从文件中读取map信息
 * @author Administrator
 *
 */
public class Demo3 {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Properties p=new Properties();
        //相对路径
        //p.load(new FileInputStream(new File("db.properties")));
        
        //绝对路径
        //p.load(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\db.properties")));
        
        
        //类路径加载资源文件
        //p.load(Demo3.class.getResourceAsStream("/com/ahd/Hashtable/db.properties"));;
        
        //线程加载
        p.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/ahd/Hashtable/db.properties"));;
        System.out.println(p.getProperty("user","ddd"));
    }
}
Demo3

 

转载于:https://www.cnblogs.com/aihuadung/p/9329682.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值