社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
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配置");
}
}
类.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"));
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!