java读取配置⽂件的⽅法
1、基于ClassLoder读取配置⽂件
注意:该⽅式只能读取类路径下的配置⽂件,有局限但是如果配置⽂件在类路径下⽐较⽅便。
InputStream inputStream1 = ResourceAsStream("filepath");
2、基于 InputStream 读取配置⽂件
注意:该⽅式的优点在于可以读取任意路径下的配置⽂件。
InputStream inputStream1 = new BufferedInputStream(new FileInputStream("filepath"));
3、举例:
(1)结构:
(2)代码
public class PropertiesUtil {
public static final PropertiesUtil INSTANCE = new PropertiesUtil();
public static PropertiesUtil getInstance(){
return INSTANCE;
}
/**
* 1.使⽤getResourceAsStream读取配置⽂件
*/
public void readPropertiesByGetSourceAsStream(){
// 1.src下⾯com/demo/config包内的配置⽂件config1.properties
InputStream inputStream1 = ClassLoader().getResourceAsStream("com/demo/config/config1.properties");
// 2.读取和PropertiesTest位于同⼀个包内的配置⽂件config2.properties
InputStream inputStream2 = ResourceAsStream("config2.properties");
// 3.读取src根⽬录下的配置⽂件config3.properties
InputStream inputStream3 = ClassLoader().getResourceAsStream("config3.properties");
// 4.读取位于另外⼀个resource⽂件夹⾥⾯的配置⽂件config4.properties
InputStream inputStream4 = ClassLoader().getResourceAsStream("config4.properties");
Properties properties = new Properties();
System.out.println("使⽤getResourceAsStream()配置⽂件...");
try {
System.out.println("-----读取inputStream1开始-----");
properties.load(inputStream1);
System.out.println("config1.properties:username = "+Property("username") + ",password = " +Property("password") +",url = " + Property("url"));            System.out.println("-----读取inputStream1结束-----");
System.out.println("---------------------------------------------");
System.out.println("-----读取inputStream2开始-----");
properties.load(inputStream2);
System.out.println("config2.properties:username = "+Property("username") + ",password = " +Property("password") +",url = " + Property("url"));            System.out.println("-----读取inputStream2结束-----");
System.out.println("---------------------------------------------");
System.out.println("-----读取inputStream3开始-----");
properties.load(inputStream3);
System.out.println("jdbc.properties:");
// 这⾥的%s是java String占位符
System.out.println(String.format("jdbc.driver=%s",Property("jdbc.driver")));
System.out.println(String.format("jdbc.url=%s",Property("jdbc.url")));
System.out.println(String.format("jdbc.username=%s",Property("jdbc.username")));
System.out.println(String.format("jdbc.password=%s",Property("jdbc.password")));
System.out.println("-----读取inputStream3结束-----");
System.out.println("---------------------------------------------");
System.out.println("-----读取inputStream4开始-----");
properties.load(inputStream4);
System.out.println("config.properties:");
System.out.println(MessageFormat.format("dbuser={0}", Property("dbuser")));
System.out.println(MessageFormat.format("dbpassword={0}", Property("dbpassword")));
System.out.println(MessageFormat.format("database={0}", Property("database")));
System.out.println("-----读取inputStream4结束-----");
System.out.println("---------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(null != inputStream1){
try {
inputStream1.close();
读取配置文件失败} catch (IOException e) {
送qq币
e.printStackTrace();
}
}
if(null != inputStream2){
try {
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != inputStream3){
try {
inputStream3.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != inputStream4){
try {
inputStream4.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 2.使⽤InputStream读取配置⽂件
*/
public void readPropertiesByInputStream(){
InputStream inputStream1 = null;
InputStream inputStream2 = null;
InputStream inputStream3 = null;
InputStream inputStream4 = null;
System.out.println("使⽤InputStream读取配置⽂件...");
try {最忧伤的网名
// 1.src下⾯com/demo/config包内的配置⽂件config1.properties
inputStream1 = new BufferedInputStream(new FileInputStream("src/com/demo/config/config1.properties"));
// 2.读取和PropertiesTest位于同⼀个包内的配置⽂件config2.properties
inputStream2 = new BufferedInputStream(new FileInputStream("src/com/demo/day8/config2.properties"));
// 3.读取src根⽬录下的配置⽂件config3.properties
inputStream3 = new BufferedInputStream(new FileInputStream("src/config3.properties"));
// 4.读取位于另外⼀个resource⽂件夹⾥⾯的配置⽂件config4.properties
inputStream4 = new BufferedInputStream(new FileInputStream("resource/config4.properties"));
Properties properties = new Properties();
try {
System.out.println("-----读取inputStream1开始-----");
properties.load(inputStream1);
System.out.println("config1.properties:username = " + Property("username") + ",password = " + Property("password") + ",url = " + Property("url"));                System.out.println("-----读取inputStream1结束-----");
System.out.println("---------------------------------------------");
System.out.println("-----读取inputStream2开始-----");
properties.load(inputStream2);
System.out.println("config2.properties:username = " + Property("username") + ",password = " + Property("password") + ",url = " + Property("url"));                System.out.println("-----读取inputStream2结束-----");
System.out.println("---------------------------------------------");
System.out.println("-----读取inputStream3开始-----");
properties.load(inputStream3);
System.out.println("jdbc.properties:");
// 这⾥的%s是java String占位符
System.out.println(String.format("jdbc.driver=%s", Property("jdbc.driver")));
System.out.println(String.format("jdbc.url=%s", Property("jdbc.url")));
System.out.println(String.format("jdbc.username=%s", Property("jdbc.username")));
System.out.println(String.format("jdbc.password=%s", Property("jdbc.password")));
System.out.println("-----读取inputStream3结束-----");
System.out.println("---------------------------------------------");
System.out.println("-----读取inputStream4开始-----");
properties.load(inputStream4);
System.out.println("config.properties:");
System.out.println(MessageFormat.format("dbuser={0}", Property("dbuser")));
System.out.println(MessageFormat.format("dbpassword={0}", Property("dbpassword")));
System.out.println(MessageFormat.format("database={0}", Property("database")));
System.out.println("-----读取inputStream4结束-----");
System.out.println("---------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != inputStream1) {
try {
inputStream1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
inputStream2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream3) {
try {
inputStream3.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream4) {
try {
inputStream4.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public class PropertiesTest {
/**
* 1.使⽤getSourceAsStream读取配置⽂件测试
*/
@Test
public void readPropertiesByGetSourceAsStreamTest(){
}
/**
* 2.使⽤InputStream读取配置⽂件测试
*/
@Test
public void readPropertiesByInputStreamTest(){
井川里予打过扑克视频Instance().readPropertiesByInputStream();
}
}
测试结果:
config4.properties不在测试类的路径下,不能通过ClassLoder的⽅式读取配置⽂件。
InputStream可以读取任何路径下的配置⽂件。
(4)读取配置⽂件中的全部
以读取config3.properties为例;
public void readAllInfo()
{
/*
* 读取src根⽬录下⾯的配置⽂件
* config3.properties
*/
InputStream is3 = ClassLoader().getResourceAsStream("config3.properties");    Properties p = new Properties();
try
{
p.load(is3);
//读取资源⽂件的所有的key值(列举)
张含韵男朋友Enumeration en= p.propertyNames();
System.out.println("读取is3开始...");
while(en.hasMoreElements())
{
String key = (String) en.nextElement();
System.out.println(key + "=" + p.getProperty(key));
}
System.out.println("读取is3结束...");
}
catch (IOException e)
{
e.printStackTrace();
System.out.println("读取资源⽂件出错");
}
finally
{
if(null != is3)
{
try
{
is3.close();
System.out.println("关闭");            }
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
测试结果:
测试结果:
[Cèshì jiéguǒ:]
Test Results: