版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/86697203
1 Hashtable
2 Hashtable 子类 Properties
- 读写资源配置文件
- 要求 键 和 值都是字符串;
package day04.pro;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class Demo1 {
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
pro.setProperty("driver","oracle.jdbc.driver.OracleDriver");
pro.setProperty("url","jdbc:oracle:thin:@localhost");
pro.setProperty("pwd","111111");
String url = pro.getProperty("url","null");
System.out.println(url);
/*
* 使用 Properties 输出到文件
* */
pro.store(new FileOutputStream(new File("D:/DB.properties")),"DB配置文件");
pro.storeToXML(new FileOutputStream(new File("D:/DB.xml")),"DB-XML配置");
}
}
2.1 类路径加载资源配置文件
类.class.getResourceAsStream("/")
Thread.currentThread().getContextClassLoader().getResourceAsStream()
package day04.pro;
import java.io.IOException;
import java.util.Properties;
public class Demo2 {
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
//类的相对路径
// pro.load(Demo2.class.getResourceAsStream("DB.properties"));
pro.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("DB.properties"));
System.out.println(pro.getProperty("url","null"));
}
}