读书人

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

发布时间: 2012-12-22 12:05:06 作者: rapoo

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

?

?

/*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();//调用不带参数的构造方法
?

?

?

?

?

?

?

?

?

?

?

?

?

?

读书人网 >编程

热点推荐