JAVA学习笔记44——其他容器简介2:HashTable+Properties

最近在看JAVA教学的视频,觉得老师讲的很好,同时借用源代码还有笔记来撰写本系列博客,记录自己的学习内容,同时也供看到的人学习。

本篇介绍的是HashTable、Properties。

首先来看HashTable:


一、Hashtable 与HashMap的区别:1、主要:Hashtable线程安全,同步,效率相对低下; HashMap线程不安全,非同步,效率相对高。2、父类:Hashtable的是 Dictionary ;HashMap的是 AbstractMap。3、null:Hashtable键与值不能为null;HashMap键最多一个null,值可以多个null。

二、Properties (HashTable的子类)

1、作用:读写资源配置文件

2、键与值只能为字符串

3、方法:

setProperty(String key, String value)

getProperty(String key) :不存在则返回空

getProperty(String key, String defaultValue):不存在则返回默认值

后缀:“.properties”(文件后缀为properties):

store(OutputStream out, Stringcomments)

store(Writer writer, String comments)

load(InputStream inStream)

load(Reader reader)

.xml(文件后缀为xml):

storeToXML(OutputStream os, Stringcomment)  :UTF-8字符集

storeToXML(OutputStream os, Stringcomment, String encoding)

loadFromXML(InputStream in)

(注:文件的后缀和文件的内容没有直接关系,只是用于区分读取软件的,用正确的软件打开才能显示正确的内容)

下面是示例代码:

NO.1:用properties来存储数据

import java.util.Properties;
/**
 * Properties 资源配置文件的读写
 * 1、key 与value 只能为字符串
 * 2、存储与读取
 * setProperty(String key, String value) 
 * getProperty(String key, String defaultValue)  
 */
public class Demo01 {
	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);
	}
}
NO.2: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 Demo02 {
	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	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.store(new FileOutputStream(new File("src/com/bjsxt/others/pro/db.properties")), "db配置");
	}
}

三、相对路径与绝对路径1、绝对路径:带盘符的路径,即..\..\...\。2、相对路径:指向当前的项目或者工程。

NO.3:使用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 in) 
 */
public class Demo03 {
	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	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/com/bjsxt/others/pro/db.properties"));
		System.out.println(pro.getProperty("user", "bjsxt"));
	}
}

四、类路径加载资源文件,类所在的根路径。

1、类.class.getResourceAsStream("/")。2、Thread.currentThread().getContextClassLoader().getResourceAsStream("")(这里的“”表示的是默认的bin目录)

NO.4:使用类相对路径读取配置文件

import java.io.IOException;
import java.util.Properties;
/**
 * 使用类相对路径读取配置文件
 *  bin  
 */
public class Demo04 {
	/**
	 * @param args
	 * @throws IOException 
	 */
	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(空即表示bin目录) 
		pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/bjsxt/others/pro/db.properties"));
		System.out.println(pro.getProperty("user", "bjsxt"));
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<--- Last few GCs ---> [11584:0000020B71203B50] 69329 ms: Scavenge (reduce) 2037.9 (2050.3) -> 2037.3 (2051.3) MB, 6.3 / 0.0 ms (average mu = 0.348, current mu = 0.419) allocation failure [11584:0000020B71203B50] 69339 ms: Scavenge (reduce) 2038.2 (2053.3) -> 2037.6 (2053.3) MB, 3.2 / 0.0 ms (average mu = 0.348, current mu = 0.419) allocation failure [11584:0000020B71203B50] 69348 ms: Scavenge (reduce) 2038.3 (2050.5) -> 2038.1 (2052.0) MB, 3.3 / 0.0 ms (average mu = 0.348, current mu = 0.419) allocation failure 12: 00007FF6482BAB44 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithFiller+84 13: 00007FF6482BAE43 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArrayWithMap+35 14: 00007FF6480C8A10 v8::internal::HashTable<v8::internal::NameDictionary,v8::internal::NameDictionaryShape>::EnsureCapacity<v8::internal::Isolate>+208 15: 00007FF6480C6086 v8::internal::Dictionary<v8::internal::NameDictionary,v8::internal::NameDictionaryShape>::Add<v8::internal::Isolate>+102 16: 00007FF6480CF346 v8::internal::BaseNameDictionary<v8::internal::NameDictionary,v8::internal::NameDictionaryShape>::Add+118 17: 00007FF647FC430C v8::internal::Runtime::GetObjectProperty+2204 18: 00007FF64848B50D v8::internal::SetupIsolateDelegate::SetupHeap+463949 19: 00007FF6485017A9 v8::internal::SetupIsolateDelegate::SetupHeap+947945 20: 00007FF648423EF2 v8::internal::SetupIsolateDelegate::SetupHeap+40498 21: 00007FF648423EF2 v8::internal::SetupIsolateDelegate::SetupHeap+40498 22: 00007FF648423EF2 v8::internal::SetupIsolateDelegate::SetupHeap+40498 23: 00007FF648423EF2 v8::internal::SetupIsolateDelegate::SetupHeap+40498 24: 00007FF648423EF2 v8::internal::SetupIsolateDelegate::SetupHeap+40498 25: 00007FF648423EF2 v8::internal::SetupIsolateDelegate::SetupHeap+40498 26: 00007FF648423EF2 v8::internal::SetupIsolateDelegate::SetupHeap+40498 27: 0000028E519B08BF
最新发布
07-22

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值