首先创建主程序,再创建一个播放器的程序,在电脑打开后运行主程序和播放器,
创建一个程序启用和关闭的接口,在每一个需要用到启用和关闭的功能时,就调用
该接口实现类,主程序需要使用这个程序时,直接调用即可。当需要加入很多程序时
就需要不断的创建调用,这个过程太过繁杂。在此为了不改变源代码的情况下,通过配置
文件来访问class文件来访问class文件显得及其优秀了。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class BrainMain {
/**
* @param args
* @throws IOException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public static void main(String[] args) {
MainRun mr = new MainRun();
mr.run();
//创建File流读取配置文件
File configFile = new File("conn.properties");
Properties pro = new Properties();
//初始化FileInputStream流的值
FileInputStream file = null;
try {
//创建 FileInputStream流来接受配置文件
file = new FileInputStream(configFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
//利用Properties读取配置文件
pro.load(file);
} catch (IOException e) {
e.printStackTrace();
}
//for循环遍历x值作为配置文件的后缀
for(int x=0; x<pro.size(); x++){
String str = pro.getProperty("conn"+(x+1));
Class clazz = null;
try {
//利用forName读取配置文件的名称
clazz = Class.forName(str);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
RunInterface r = null;
try {
//创建新的实例
r = (RunInterface)clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
//调用主程序的运行功能
mr.runInter(r);
}
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
下面是配置文件截图
下面是程序运行截图