import java.io.*;
import java.lang.reflect.Method;
import java.util.Properties;
public class Text7 {
/**写一个Properties格式的配置文件,用反射 的方式运行run方法
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
read();
}
public static void read() throws Exception{
Properties prop = new Properties();
File file = new File("read.properties");
if(!file.exists())
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException("创建失败");
}
FileInputStream fis = new FileInputStream(file);
System.out.println("1");
prop.setProperty("name","DemoClass");
prop.load(fis);
FileOutputStream fos = new FileOutputStream(file);
prop.store(fos, "");
String cls=prop.getProperty("name");
System.out.println(cls);
if(cls!=null){
System.out.println("3");
//Class clazz =cls.getClass();
System.out.println("4");
Method mod =cls.getClass().getMethod("run",null);//这里 无法通过
System.out.println("5");
mod.invoke(cls, null);
System.out.println("6");
}
}
}
class DemoClass {
public void run()
{
System.out.println("welcome to heima!");
}
}
|