package run;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
public class Mybatis {
public static void save(Object obj){
try (
PrintStream ps =new PrintStream(new FileOutputStream("F:\JavaProject\Project\src\app.txt",true))
){
Class c = obj.getClass();
ps.println("-----------" + c.getSimpleName() + "------------");
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
String name = field.getName();
field.setAccessible(true);
String value = field.get(obj) + "";
ps.println(name + "=" + value);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package run;
public class FileDemo {
public static void main(String[] args) {
Student student = new Student("孙悟空",10000,155.0,"斜月三星洞");
Mybatis.save(student);
Teacher teacher = new Teacher("菩提老祖",10000000,175.0,"斜月三星洞");
Mybatis.save(teacher);
}
}