社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
该方式必须知道文件的真实路径。
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());
}
}
下面的方式的是相对路径,目录是相对当前目录而言的。这种方式也可以使用绝对路径,绝对路径需要加上斜杠(/)。不管是绝对还是相对路径,底层调用都是类加载器。
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());
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!