28 用类加载器的方式管理资源和配置文件

来源:互联网 发布:2017年冷链物流数据 编辑:程序博客网 时间:2024/05/22 05:20

 

 

/*getRealPath();//金山词霸/内部一定要记住用完整的路径,但完整的路径不是硬编码,而是运算出来的。*///方法一:能读写,写时  OutputStream fos = new FileOutputStream(filePath);。默认是从项目目录下开始//InputStream inStream=new FileInputStream("config.properties");//方法二:只读。找到雷加载器getClassLoader(),默认是从src目录开始//InputStream inStream=ReflectTest2.class.getClassLoader().getResourceAsStream("com/zyj/day1/resources/config.properties");//方法三:只读。默认是从ReflectTest2包目录开始,可以用相对目录//InputStream inStream=ReflectTest2.class.getResourceAsStream("resources/config.properties");//方法四:只读。默认是从ReflectTest2包目录开始,可以用绝对目录,前面加一个"/".用绝对路径是是从src目录开始InputStream inStream=ReflectTest2.class.getResourceAsStream("/com/zyj/day1/resources/config.properties");Properties p=new Properties();p.load(inStream);p.clone();String className=p.getProperty("className");Collection<ReflectPoint> collections=(Collection<ReflectPoint>) Class.forName(className).newInstance();//调用不带参数的构造方法
 

 

 

 

 

 

 

 

 

 

 

 

 

 

阅读全文
0 0
原创粉丝点击