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());
	}
}

发布了37 篇原创文章 · 获赞 30 · 访问量 1131

猜你喜欢

转载自blog.csdn.net/myjess/article/details/104375339