Java中加载配置文件的三种方式 - Go语言中文社区

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


一、通过文件路径加载

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

1、配置文件放置位置

2、具体代码如下

package cn.sunft.day01.reflect;

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

/**
 * 
 * @author sunft
 *
 */
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;

/**
 * 
 * @author sunft
 *
 */
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;

/**
 * 
 * @author sunft
 *
 */
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;

/**
 * 
 * @author sunft
 *
 */
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());
	}

}

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/BruceLeeNumberOne/article/details/82750749
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:13:03
  • 阅读 ( 1020 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢