java读取属性文件路径_java读取配置文件的方法

public classPropertiesUtil {public static final PropertiesUtil INSTANCE = newPropertiesUtil();public staticPropertiesUtil getInstance(){returnINSTANCE;

}/*** 1.使用getResourceAsStream读取配置文件*/

public voidreadPropertiesByGetSourceAsStream(){//1.src下面com/demo/config包内的配置文件config1.properties

InputStream inputStream1 = PropertiesTest.class.getClassLoader().getResourceAsStream("com/demo/config/config1.properties");//2.读取和PropertiesTest位于同一个包内的配置文件config2.properties

InputStream inputStream2 = PropertiesTest.class.getResourceAsStream("config2.properties");//3.读取src根目录下的配置文件config3.properties

InputStream inputStream3 = PropertiesTest.class.getClassLoader().getResourceAsStream("config3.properties");//4.读取位于另外一个resource文件夹里面的配置文件config4.properties

InputStream inputStream4 = PropertiesTest.class.getClassLoader().getResourceAsStream("config4.properties");

Properties properties= newProperties();

System.out.println("使用getResourceAsStream()配置文件...");try{

System.out.println("-----读取inputStream1开始-----");

properties.load(inputStream1);

System.out.println("config1.properties:username = "+properties.getProperty("username") + ",password = " +properties.getProperty("password") +",url = " + properties.getProperty("url"));

System.out.println("-----读取inputStream1结束-----");

System.out.println("---------------------------------------------");

System.out.println("-----读取inputStream2开始-----");

properties.load(inputStream2);

System.out.println("config2.properties:username = "+properties.getProperty("username") + ",password = " +properties.getProperty("password") +",url = " + properties.getProperty("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",properties.getProperty("jdbc.driver")));

System.out.println(String.format("jdbc.url=%s",properties.getProperty("jdbc.url")));

System.out.println(String.format("jdbc.username=%s",properties.getProperty("jdbc.username")));

System.out.println(String.format("jdbc.password=%s",properties.getProperty("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}", properties.getProperty("dbuser")));

System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));

System.out.println(MessageFormat.format("database={0}", properties.getProperty("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();

}

}if(null !=inputStream2){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();

}

}

}

}/*** 2.使用InputStream读取配置文件*/

public voidreadPropertiesByInputStream(){

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= newProperties();try{

System.out.println("-----读取inputStream1开始-----");

properties.load(inputStream1);

System.out.println("config1.properties:username = " + properties.getProperty("username") + ",password = " + properties.getProperty("password") + ",url = " + properties.getProperty("url"));

System.out.println("-----读取inputStream1结束-----");

System.out.println("---------------------------------------------");

System.out.println("-----读取inputStream2开始-----");

properties.load(inputStream2);

System.out.println("config2.properties:username = " + properties.getProperty("username") + ",password = " + properties.getProperty("password") + ",url = " + properties.getProperty("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", properties.getProperty("jdbc.driver")));

System.out.println(String.format("jdbc.url=%s", properties.getProperty("jdbc.url")));

System.out.println(String.format("jdbc.username=%s", properties.getProperty("jdbc.username")));

System.out.println(String.format("jdbc.password=%s", properties.getProperty("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}", properties.getProperty("dbuser")));

System.out.println(MessageFormat.format("dbpassword={0}", properties.getProperty("dbpassword")));

System.out.println(MessageFormat.format("database={0}", properties.getProperty("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();

}

}if (null !=inputStream2) {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 classPropertiesTest {/*** 1.使用getSourceAsStream读取配置文件测试*/@Testpublic voidreadPropertiesByGetSourceAsStreamTest(){

PropertiesUtil.getInstance().readPropertiesByGetSourceAsStream();

}/*** 2.使用InputStream读取配置文件测试*/@Testpublic voidreadPropertiesByInputStreamTest(){

PropertiesUtil.getInstance().readPropertiesByInputStream();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值