对于properties文件,有以下要求:

1,文件位置:任意,建议放在src下

2,文件名称:任意,扩展名为properties

3,文件内容:一行一组数据,格式是“key=value”,行末最好不要加空格,回车

    a) key命名自定义,如果是多个单词,习惯使用点分隔;

    b) value值不支持中文,如果是需要使用非英文字符,将进行unicode转换

properties文件

pci1 = reflectDemo2.SoundCard
pci2 = reflectDemo2.NetCard


PCI接口

package reflectDemo2;

public interface PCI {

public void start();

public void stop();
}


SoundCard类

package reflectDemo2;

public class SoundCard implements PCI {

@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("声卡启动。。。");
}

@Override
public void stop() {
// TODO Auto-generated method stub
System.out.println("声卡关闭。。。");
}

}


NetCard类

package reflectDemo2;

public class NetCard implements PCI {

@Override
public void start() {
// TODO Auto-generated method stub
System.out.println("网卡启动。。。");
}

@Override
public void stop() {
// TODO Auto-generated method stub
System.out.println("网卡关闭。。。");
}

}


MainBoard类

package reflectDemo2;

public class MainBoard {

public void run() {
System.out.println("主板运行。。。");
}

public void usePCI(PCI p) {
p.start();
p.stop();
}
}


ReflectTest类

package reflectDemo2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;

import org.junit.jupiter.api.Test;

public class ReflectTest {

/*
* 使用ResourceBundle类加载properties文件 此时properties文件作为参数,只需要文件名,不需要peoperties后缀
* 且properties文件的路径最好在src下
*/
@Test
public void test1() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
MainBoard mb = new MainBoard();
mb.run();
// System.out.println(System.getProperty("user.dir")); //user.dir指定了当前的路径
ResourceBundle bundle = ResourceBundle.getBundle("pci2");
// 获取所有的键
Set<String> keys = bundle.keySet();
// 遍历 key 得到对应的 value
for (String key : keys) {
String value = bundle.getString(key);
Class clazz = Class.forName(value);
PCI pci = (PCI) clazz.newInstance();
mb.usePCI(pci);
}
}

/*
* 文件读取加载properties文件
*/
@Test
public void test2() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
MainBoard mb = new MainBoard();
mb.run();
FileInputStream fis = new FileInputStream("./src/reflectDemo2/pci2.properties");
Properties prop = new Properties();
prop.load(fis);

for (int i = 0; i < prop.size(); i++) {
String className = prop.getProperty("pci" + (i + 1));
Class clazz = Class.forName(className);
PCI pci = (PCI) clazz.newInstance();
mb.usePCI(pci);
}
}


/*
* 类加载器读取properties文件,类加载器是专门加载classes下的文件的
* getResource()方法的参数是一个相对地址 是相对于classes
*/
@Test
public void test3() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
MainBoard mb = new MainBoard();
mb.run();
InputStream is = ReflectTest.class.getClassLoader().getResourceAsStream("pci2.properties");
Properties prop = new Properties();
prop.load(is);
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
String className = prop.getProperty(key);
Class clazz = Class.forName(className);
PCI pci = (PCI) clazz.newInstance();
mb.usePCI(pci);
}
}
}