131_容器_HashTable与Properties_绝对相对类路径存储与读取

这里写图片描述

  • HashMap_HashTable.txt
Hashtable与HashMap区别:
1)主要:Hashtable线程安全,同步,效率低下
      HashMap线程不安全,非同步,效率相对高
2)父类:Hashtable extends Dictionary;HashMap extends AbstractMap
3)null:Hashtable键与值不能为null
       HashMap键最多一个null,值可以多个null.
  • PropertiesTest.java
package collection.others.HashMap_HashTable_Properties;

import java.util.Properties;

/**Properties
 * 作用:读取资源配置文件
 * 1、key 与value 只能为字符串
 * 2、存储与读取
 * setProperty(String key,String value);
 * getProperty(String key);
 * getProperty(String key,String defaultValue);
 */
public class ProperitiesTest {
    public static void main(String[] args) {
        //创建对象
        Properties pro =new Properties();
        //存储
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");
        //获取
        String url =pro.getProperty("url","test");
        System.out.println(url);
    }
}
  • PropertiesStore.java
package collection.others.HashMap_HashTable_Properties;

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

/**
 * 使用Properties输出到文件
 * 资源配置文件:
 * 1、.properties
 *  store(OutputStream out, String comments) 
    store(Writer writer, String comments) 
   2、.xml
   storeToXML(OutputStream os, String comment) :UTF-8字符集
   storeToXML(OutputStream os, String comment, String encoding) 
 */

public class PropertiesStore {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        //创建对象
        Properties pro =new Properties();
        //存储
        pro.setProperty("driver", "oracle.jdbc.driver.OracleDriver");
        pro.setProperty("url", "jdbc:oracle:thin:@localhost:1521:orcl");
        pro.setProperty("user", "scott");
        pro.setProperty("pwd", "tiger");

        //存储到e:/others  绝对路径盘符:
        //pro.store(new FileOutputStream(new File("e:/others/db.properties")), "db配置");
        //pro.storeToXML(new FileOutputStream(new File("e:/others/db.xml")), "db配置");
        //使用相对路径 当前的工程
//      pro.store(new FileOutputStream(new File("db.properties")), "db配置");
        pro.store(new FileOutputStream(new File("src/db.properties")), "db配置");
        pro.storeToXML(new FileOutputStream(new File("src/db.properties")), "db配置");
//      pro.store(new FileOutputStream(new File("src/collection/others/Enumeration/db.properties")), "db配置");
    }
}
  • Propertiesload.java
package collection.others.HashMap_HashTable_Properties;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * 使用Properties读取配置文件
 * 资源配置文件:
 * 使用相对与绝对路径读取
 * load(InputStream inStream) 
   load(Reader reader) 
   loadFromXML(InputStream inStream) 
 */

public class Propertiesload {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Properties pro=new Properties();
        //读取 绝对路径
        //pro.load(new FileReader("e:/others/db.properties"));
        //读取 相对路径
        pro.load(new FileReader("src/db.properties"));
        System.out.println(pro.getProperty("user", "bjsxt"));
    }
}
  • PropertiesClassPath.java
package collection.others.HashMap_HashTable_Properties;

import java.io.IOException;
import java.util.Properties;

/**
 * 使用类相对路径读取配置文件
 *  bin  
 */
public class PropertiesClassPath {
    public static void main(String[] args) throws IOException {
        Properties pro =new Properties();
        //类相对路径  "/"表示bin根目录 
        //pro.load(Demo04.class.getResourceAsStream("/com/bjsxt/others/pro/db.properties"));
        //""表示bin根目录
        pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
        System.out.println(pro.getProperty("user", "bjsxt"));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值