最近在看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"));
}
}