反射的应用,读取properties配置文件中的数据(普通InputStream方式,类加载方式),再调用运行



package com.itheima;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * 8、 已知一个类,定义如下:
   package cn.itcast.heima;
      public class DemoClass {
      public void run()
      {
         System.out.println("welcome to heima!");
      }   
     }
(1) 写一个Properties格式的配置文件,配置类的完整名称。
(2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,用反射 的方式运行run方法。
 * 
 * @author 王建伟
 *
 */
public class Test8 {
	public static void main(String[] args) throws Exception {
//		method();
		method1();
	}
	public static void method() throws Exception{
		Properties pro=new Properties();
//--------用InputStream加载配置文件-----------------------------------------------------------------------------		
		//写了相对路径加载Test8.properties
		//用FileInputStream,这三种路径都可以
		FileInputStream fis1=new FileInputStream(new File("src/com/itheima/Test8.properties"));
		FileInputStream fis2=new FileInputStream(new File("/com/itheima/Test8.properties"));
		FileInputStream fis3=new FileInputStream(new File("com/itheima/Test8.properties"));
		
//		pro.load(fis1);//加载properties文件
//		fis1.close();//关闭流
		
//		pro.load(fis2);//加载properties文件
//		fis2.close();//关闭流
		
//		pro.load(fis3);//加载properties文件
//		fis3.close();//关闭流
//----------用加载器getClassLoader()加载--------------------------------------------------------------------------		
		//用类加载期的方式加载properties文件
		InputStream is1=Test8.class.getClassLoader().getResourceAsStream("src/com/itheima/Test8.properties");
		InputStream is2=Test8.class.getClassLoader().getResourceAsStream("/com/itheima/Test8.properties");
		InputStream is3=Test8.class.getClassLoader().getResourceAsStream("com/itheima/Test8.properties");
		
//		pro.load(is1);//加载properties文件
//		is1.close();//关闭流
//		
//		pro.load(is2);//加载properties文件
//		is2.close();//关闭流
//		
//		pro.load(is3);//加载properties文件
//		is3.close();//关闭流
//---------直接用类文件加载,其实里面就是封装了getClassLoader()方法----------------------------------------------------------------------------				
		
		InputStream iss1=Test8.class.getResourceAsStream("Test8.properties");//可以用相对路径
		InputStream iss2=Test8.class.getResourceAsStream("resource/Test8.properties");//可以用相对路径,在下一级文件夹中
		InputStream iss3=Test8.class.getResourceAsStream("com/itheima/resource/Test8.properties");//也可以用绝对路径,这三种路径都是可以 的
		InputStream iss4=Test8.class.getResourceAsStream("/com/itheima/resource/Test8.properties");//也可以用绝对路径,这三种路径都是可以 的
		InputStream iss5=Test8.class.getResourceAsStream("src/com/itheima/resource/Test8.properties");//也可以用绝对路径,这三种路径都是可以 的
		
//		pro.load(iss1);//加载properties文件
//		iss1.close();//关闭流
		
//		pro.load(iss2);//加载properties文件
//		iss2.close();//关闭流
		
		pro.load(iss5);//加载properties文件
		iss5.close();//关闭流
		
		String path=(String)pro.get("className");//因为我定义的key,我就直接通过可以获取值了
		System.out.println("-----------"+path);//打印获取出来的value,即类的路径
		Class<?> cla=Class.forName(path);//通过字符串加载类
		Method m=cla.getMethod("run");//调用类中的方法
		m.invoke(null);//执行
	}
	public static void method1() throws Exception{
		Properties p=new Properties();
		InputStream is=new FileInputStream(new File("src/com/itheima/Test8.properties"));
		p.load(is);
		is.close();
		String className=p.getProperty("className");
		DemoClass name=(DemoClass)Class.forName(className).newInstance();
		name.run1();
	}
}
//根据题意,我改动了一下题目中的类
class DemoClass {
    public static void run(){
       System.out.println("welcome to heima!");
    }
    public void run1(){
        System.out.println("welcome to heima!!!!!!!!!!!!!!!!!!");
     }
}






对应properties文件中的内容为:
className=com.itheima.DemoClass



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King·Forward

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值