一、保存信息
Student类:
package testpackage;
import java.io.IOException;
public class Student {
private String name;
private String area;
public String testfield;
private int age;
public Student() {
}
public Student(String name, String area, int age) {
this.name = name;
this.area = area;
this.age = age;
}
private Student(String name){
this.name=name;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return area
*/
public String getArea() {
return area;
}
/**
* 设置
* @param area
*/
public void setArea(String area) {
this.area = area;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student";
}
public void eat(String something) throws IOException,NullPointerException {
System.out.println("在吃"+something);
}
public void eat(int number){
System.out.println("吃了"+number+"个东西");
}
}
Teacher类:
package testpackage;
public class Teacher {
private String name;
private String job;
private int age;
private String area;
public Teacher() {
}
public Teacher(String name, String job, int age, String area) {
this.name = name;
this.job = job;
this.age = age;
this.area = area;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return job
*/
public String getJob() {
return job;
}
/**
* 设置
* @param job
*/
public void setJob(String job) {
this.job = job;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return area
*/
public String getArea() {
return area;
}
/**
* 设置
* @param area
*/
public void setArea(String area) {
this.area = area;
}
public String toString() {
return "Teacher";
}
}
主函数:
import testpackage.Student;
import testpackage.Teacher;
import java.io.*;
import java.lang.reflect.Field;
public class Test {
public static void main(String[] args) throws IllegalAccessException, IOException {
Teacher teacher=new Teacher("历山","班主任",20,"南京");
Student stu=new Student("张三","北京",16);
SaveObject(teacher);
SaveObject(stu);
}
public static void SaveObject(Object object) throws IllegalAccessException, IOException {
//将对象的所有成员变量及对应属性保存到本地文件中去 name=XX
//针对对象来创建.class文件
Class clazz = object.getClass();
//将每一个信息存入本地文件中
String str=object+"";
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(str+".txt")));
//先获取对象所有的成员变量
Field[] declaredFields = clazz.getDeclaredFields();
for (Field declaredfield : declaredFields) {
//遍历所有的成员变量
//临时取消权限检验
declaredfield.setAccessible(true);
//获取成员变量的名字
String name = declaredfield.getName();
//获取成员变量对应的值
Object o = declaredfield.get(object);
String info=name+"="+o;
bw.write(info);
bw.newLine();
bw.flush();
}
bw.close();
}
}
二、跟配置文件结合动态创建
配置文件:
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
public class Test2 {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//动态创建对象并调用方法
//读取配置文件
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("prop.properties")));
String b;
//创建prop集合
Properties prop=new Properties();
//读取配置文件
prop.load(br);
br.close();
System.out.println(prop); //{classname=testpackage.Student, method=study}
//查找对应键的值(全类名)
//方法返回值是Object类
System.out.println(prop.get("classname")); //testpackage.Student
Class clazz = Class.forName((String) prop.get("classname"));
//查找对应键的值(方法名)
String method =(String)prop.get("method");
Method declaredMethod = clazz.getDeclaredMethod(method);
//临时取消权限检验
declaredMethod.setAccessible(true);
//利用Clazz对象创建对应类的对象
//获取构造方法
/* Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
for (Constructor d : declaredConstructors) {
System.out.println(d);
}
*/
//获取无参构造方法
Constructor cons = clazz.getDeclaredConstructor();
//根据返回的构造方法创建对象
//临时取消权限检验
cons.setAccessible(true);
Object o = cons.newInstance();
//调用方法 使用newInstance创建的对象进行调用
declaredMethod.invoke(o);
}
}