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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yjiang_09 中级黑马   /  2014-8-31 15:13  /  948 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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;发现还是不行,还是找不到,我又对比了下,才发现还多了个分号。所以现在我想问个问题:它这里配置格式是怎么样的?为什么不能加引号,分号。原理是什么呢?谢谢!




3 个回复

倒序浏览
配置格式应该就是键值对的格式,读取的时候只能读取改格式的配置,记住就行了
回复 使用道具 举报
   properties对象存储数据是以map形式存入的,等号前面表示map中键值对的键 Key,等号后面的表示Value,而以输入路径通过反射来查找这个类的话他是不能识别路径名后面的‘;’的。所以他会出错。
回复 使用道具 举报
不觉明历
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马