Java中加载配置文件的三种方式

一、通过文件路径加载

该方式必须知道文件的真实路径

1、配置文件放置位置
在这里插入图片描述
2、具体代码如下


package cn.sunft.day01.reflect;
 
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
 
public class ReflectCollection {

 	public static void main(String[] args) throws Exception {

		InputStream ips = new FileInputStream("config.properties");
		Properties props = new Properties();
		props.load(ips);
		ips.close();
		String className = props.getProperty("className");

		//这里默认会调用无参数的构造方法

		Collection collections = (Collection) Class.forName(className).newInstance();													
		
		ReflectPoint pt1 = new ReflectPoint(3, 3);
		ReflectPoint pt2 = new ReflectPoint(5, 5);
		ReflectPoint pt3 = new ReflectPoint(3, 3);
	
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);		

		System.out.println(collections.size());
	} 
}

二、直接通过getResourceAsStream进行加载

下面的方式的是相对路径,目录是相对当前目录而言的。这种方式也可以使用绝对路径,绝对路径需要加上斜杠(/)。不管是绝对还是相对路径,底层调用都是类加载器
1、配置文件位置
类加载器

2、采用相对路径的方式,具体代码如下


package cn.sunft.day01.reflect;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;

public class ReflectCollection {
	public static void main(String[] args) throws Exception {
		//配置文件需要放在当前包目录下

		InputStream ips = ReflectCollection.class.getResourceAsStream("resources/config.properties");	
				
		Properties props = new Properties();
		props.load(ips);
		ips.close();
		String className = props.getProperty("className");

		//这里默认会调用无参数的构造方法
		Collection collections = (Collection) Class.forName(className).newInstance();
									
		ReflectPoint pt1 = new ReflectPoint(3, 3);
		ReflectPoint pt2 = new ReflectPoint(5, 5);
		ReflectPoint pt3 = new ReflectPoint(3, 3);

		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);
		
		System.out.println(collections.size());

	}
}

3、采用绝对路径的方式


package cn.sunft.day01.reflect;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;
public class ReflectCollection {

	public static void main(String[] args) throws Exception {
		//绝对路径的方式
		InputStream ips = ReflectCollection.class.getResourceAsStream(
							"/cn/sunft/day01/reflect/resources/config.properties");
						
		Properties props = new Properties();
		props.load(ips);
		ips.close();
		String className = props.getProperty("className");

		//这里默认会调用无参数的构造方法
		Collection collections = (Collection) Class.forName(className).newInstance();
				
		ReflectPoint pt1 = new ReflectPoint(3, 3);
		ReflectPoint pt2 = new ReflectPoint(5, 5);
		ReflectPoint pt3 = new ReflectPoint(3, 3);
		
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);

		System.out.println(collections.size());

	}
}

三、通过类加载的方式进行加载

在classpath所在的根目录下查找文件,注意这里的文件需要直接放在classpath指定的目录下,另外目录最前面不要加斜杠(/)

1、配置文件放置的路径
在这里插入图片描述
2、具体代码如下


package cn.sunft.day01.reflect;

import java.io.InputStream;
import java.util.Collection;
import java.util.Properties;

public class ReflectCollection {
	public static void main(String[] args) throws Exception {		
		//在classpath所在的根目录下查找文件
		//注意这里的文件需要直接放在classpath指定的目录下,
		//另外目录最前面不要加/,通过这种方式文件通常不需要修改
		InputStream ips = ReflectCollection.class.getClassLoader()			
		.getResourceAsStream("cn/sunft/day01/reflect/config.properties");

		Properties props = new Properties();
		props.load(ips);
		ips.close();
		String className = props.getProperty("className");

		//这里默认会调用无参数的构造方法
		Collection collections = (Collection) Class.forName(className).newInstance();
				
		ReflectPoint pt1 = new ReflectPoint(3, 3);
		ReflectPoint pt2 = new ReflectPoint(5, 5);
		ReflectPoint pt3 = new ReflectPoint(3, 3);
		
		collections.add(pt1);
		collections.add(pt2);
		collections.add(pt3);
		collections.add(pt1);

		System.out.println(collections.size());
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值