Java configuration file is loaded in three ways

First, by loading the file path

The file must know the way true path .

1, the configuration file placement
Here Insert Picture Description
2, specific code as follows


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

Second, directly loaded by getResourceAsStream

The following is a way relative path, in terms relative to the current directory is the directory. This way you can also use absolute paths, the absolute paths need to add a slash (/). Whether absolute or relative path, the underlying calls are class loader
1, profile location
Class loader

2, by way of a relative path, specific code as follows


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, using an absolute path way


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

	}
}

Third, the class is loaded through the loading manner

Look in the root directory of the classpath where the file, note the file here need to be placed directly on the classpath specified directory , another directory foremost do not slash (/)

1, the configuration file placement path
Here Insert Picture Description
2, specific code as follows


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

Published 37 original articles · won praise 30 · views 1131

Guess you like

Origin blog.csdn.net/myjess/article/details/104375339