本文主要是介绍java---从文件读取信息,并用反射创建对象存入对象集合,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
反射实战
从xml文件读取到对象的相关信息,根据xml文件中给出的对象类的包名,利用反射创建一个对象,并添加到集合中
Student类
package com.situ.chapterwork;public class Student {private String id;private String name;private String sex;private int age;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}private Student(String id, String name, String sex, int age) {this.id = id;this.name = name;this.sex = sex;this.age = age;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]";}}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<class><students><student class="com.situ.chapterwork.Student" stuid="001"><name>张三</name><sex>男</sex><age>22</age></student><student class="com.situ.chapterwork.Student" stuid="002"><name>李四</name><sex>女</sex><age>18</age></student></students>
</class>
FoundNewStudent类
package com.situ.chapterwork;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;public class FoundNewStudent {public static void main(String[] args) {List<Student> studentslist = new ArrayList<>();File file = new File("d:/Test/1102.xml");InputStream is = null;try {is = new FileInputStream(file);} catch (FileNotFoundException e2) {e2.printStackTrace();}SAXReader reader = new SAXReader();Document doc = null;try {doc = reader.read(is);Element root = doc.getRootElement();Element students = root.element("students");List<Element> stulist = students.elements();for(Element e : stulist) {String id = e.attributeValue("stuid");//获取student标签的stuid属性值String classname = e.attributeValue("class");//获取student标签的class属性值String name = e.element("name").getText();//获取student标签内的name标签的值String sex = e.element("sex").getText();//获取student标签内的sex标签的值int age = Integer.valueOf(e.element("age").getText()).intValue();//String转整型Class<?> clazz = Class.forName(classname);//获取classname包的类型Constructor<?> con = clazz.getDeclaredConstructor(String.class,String.class,String.class,int.class);//注意参数是否与包里的构造方法的参数对应con.setAccessible(true);//设置访问权限Student stu = (Student) con.newInstance(id,name,sex,age);//new一个student对象,并利用con.newInstance()给对象的属性赋值con.setAccessible(false);//关闭访问权限System.out.println(stu);//打印看看,对象的属性是否赋值studentslist.add(stu);//存到学生集合中}} catch (DocumentException e1) {e1.printStackTrace();} catch (ClassNotFoundException e1) {e1.printStackTrace();} catch (NoSuchMethodException e1) {e1.printStackTrace();} catch (SecurityException e1) {e1.printStackTrace();} catch (InstantiationException e1) {e1.printStackTrace();} catch (IllegalAccessException e1) {e1.printStackTrace();} catch (IllegalArgumentException e1) {e1.printStackTrace();} catch (InvocationTargetException e1) {e1.printStackTrace();}finally {try {is.close();} catch (IOException e) {e.printStackTrace();}}}}
运行效果:
虽然jdk1.7就对异常做了优化,但是仅仅是对IO,不用自己手动关闭流,仅仅方便了程序员一点点,但是依然是异常地狱,现在又出现了一个新的异常地狱,即反射。
这篇关于java---从文件读取信息,并用反射创建对象存入对象集合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!