A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wangyuyus 中级黑马   /  2013-11-10 19:15  /  969 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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!");
    }   
   }

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

3 个回复

倒序浏览
本帖最后由 敗唫①輩ふ 于 2013-11-11 11:10 编辑

public class Demo1 {

        public static void main(String[] args) throws Exception {
                Demo1.read();
        }
        public static void read() throws Exception{
                 Properties p=new Properties();  
             InputStream is=new FileInputStream(new File("src/com/itheima/read.properties"));  
             p.load(is);  
             is.close();  
             String className=p.getProperty("name");  
             DemoClass name=(DemoClass)Class.forName(className).newInstance();  
             name.run();
        }

}
class DemoClass {
    public void run()
    {
       System.out.println("welcome to heima!");
    }   
}

兄弟,没那么复杂,而且你的输出流是干什么的,没有用。通过String className=p.getProperty("name");  得到name以后通过反射就得到这个类了,然后调用方法就ok了。你可以试一下上面这个改过之后的。

评分

参与人数 1技术分 +1 收起 理由
黄炳期 + 1

查看全部评分

回复 使用道具 举报
顶楼上{:soso_e130:}
回复 使用道具 举报
如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89313-1-1.html
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马