黑马程序员技术交流社区

标题: 张老师框架那节一个小问题 [打印本页]

作者: yjiang_09    时间: 2014-8-31 15:13
标题: 张老师框架那节一个小问题
  1. package com.sf.day1;

  2. import java.io.FileInputStream;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.HashSet;
  7. import java.util.Properties;

  8. public class ReflectTest2 {

  9.         /**
  10.          * @param args
  11.          */
  12.         public static void main(String[] args) throws Exception{
  13.                 // TODO Auto-generated method stub
  14.                 //Collection   collection=new HashSet();
  15.                 InputStream iStream=new FileInputStream("config.properties");
  16.                 Properties properties=new Properties();
  17.                 properties.load(iStream);
  18.                 iStream.close();
  19.                 String className=properties.getProperty("className");
  20.                 Class cl=Class.forName(className);
  21.                 Collection collection=(Collection)cl.newInstance();
  22.                 ReflectPoint aPoint=new ReflectPoint(3, 4);
  23.                 ReflectPoint bPoint=new ReflectPoint(4, 4);
  24.                 ReflectPoint cPoint=new ReflectPoint(5, 4);
  25.                 ReflectPoint dPoint=new ReflectPoint(3, 4);
  26.                 collection.add(aPoint);
  27.                 collection.add(bPoint);
  28.                 collection.add(cPoint);
  29.                 collection.add(dPoint);
  30.                 collection.add(aPoint);

  31.                 System.out.println(collection.size());

  32.         }

  33. }
复制代码
  1. className=java.util.ArrayList
复制代码
看视频好像看完了都明白了,其实一定要自己敲一遍,区别其实很大的,你看我自己敲就出现了好多小问题,比如配置文件config.properties里我自己敲的时候习惯性的敲成了className="java.util.ArrayList;"结果自然是找不到,然后我又回顾了下视频老师怎么写的,然后我改成了className=java.util.ArrayList;发现还是不行,还是找不到,我又对比了下,才发现还多了个分号。所以现在我想问个问题:它这里配置格式是怎么样的?为什么不能加引号,分号。原理是什么呢?谢谢!





作者: Java黑马    时间: 2014-8-31 15:33
配置格式应该就是键值对的格式,读取的时候只能读取改格式的配置,记住就行了
作者: 怀念黑海岸    时间: 2014-8-31 20:37
   properties对象存储数据是以map形式存入的,等号前面表示map中键值对的键 Key,等号后面的表示Value,而以输入路径通过反射来查找这个类的话他是不能识别路径名后面的‘;’的。所以他会出错。

作者: iefegend    时间: 2014-8-31 21:15
不觉明历




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2