java框架BeanUtils及路径问题练习

时间:2022-12-14 13:58:13

内省----->一个变态的反射
   BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中。  使从文件中读取的数据往对象中赋值更加简单;
 
  BeanUtils的好处:
  1. BeanUtils设置属性值的时候,如果属性是基本数据 类型,BeanUtils会自动帮我转换数据类型。
   2. BeanUtils设置属性值的时候底层也是依赖于get或者Set方法设置以及获取属性值的。
   3. BeanUtils设置属性值,如果设置的属性是其他的引用 类型数据,那么这时候必须要注册一个类型转换器。

代码练习:

Emp类:

package com.java.base;

import java.sql.Date;

public class Emp {

    private int id;

    private String name;

    private double salary;

    private Date birthday;

    public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public Emp(int id, String name, double salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getSalary() {
return salary;
} public void setSalary(double salary) {
this.salary = salary;
} public Emp(){} @Override
public String toString() {
return "编号: " + this.id + "姓名: " + this.name + "薪水: " + this.salary + "出生日期: " + this.birthday;
} }

BeanUtils代码:

package com.java.fram;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor; import com.java.base.Emp;
import com.java.base.Person;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter; /*
内省----->一个变态的反射
BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中。 BeanUtils的好处:
1. BeanUtils设置属性值的时候,如果属性是基本数据 类型,BeanUtils会自动帮我转换数据类型。
2. BeanUtils设置属性值的时候底层也是依赖于get或者Set方法设置以及获取属性值的。
3. BeanUtils设置属性值,如果设置的属性是其他的引用 类型数据,那么这时候必须要注册一个类型转换器。 */ public class BeanUtilsExercise { public static void main(String[] args) throws Exception {
// writeFile();
// Person p = (Person)getInstance();
// System.out.println(p);
String id = "001";
String name = "张三";
String salary = "1000";
String birthday = "2013-12-10"; //注册一个类型转换器
ConvertUtils.register(new Converter() { @Override
public Object convert(Class type, Object value) {
Date date = null;
try{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
date = (Date) dateFormat.parse((String)value);
}catch(Exception e){
e.printStackTrace();
}
return date;
}
}, Date.class); Emp e = new Emp();
BeanUtils.setProperty(e, "id", id);
BeanUtils.setProperty(e, "name", name);
BeanUtils.setProperty(e, "salary", salary);
BeanUtils.setProperty(e, "birthday", birthday); System.out.println(e);
} public static void writeFile() throws Exception{
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("obj.txt"));
bufferedWriter.write("com.java.base.Person");
bufferedWriter.newLine();
bufferedWriter.write("id=1");
bufferedWriter.newLine();
bufferedWriter.write("name=王五");
bufferedWriter.newLine();
bufferedWriter.write("age=25");
bufferedWriter.newLine();
bufferedWriter.close(); }
public static Object getInstance() throws Exception{
writeFile();
BufferedReader bufferedReader = new BufferedReader(new FileReader("obj.txt"));
String className = bufferedReader.readLine();
Class clazz = Class.forName(className);
Constructor constructor = clazz.getConstructor(null);//获取构造方法;
Object person = constructor.newInstance(null);
//通过构造方法获取一个实例
String line = null;
while((line = bufferedReader.readLine()) != null){
String[] datas = line.split("=");
Field field = clazz.getDeclaredField(datas[0]);
if(field.getType() == int.class){
field.set(person, Integer.parseInt(datas[1]));
}
else{
field.set(person, datas[1]);
}
}
return person; } }

路径问题:

绝对路径:一个文件的完整路径信息。一般绝对路径是包含有盘符 的。  绝对路径的缺陷: 因为绝对路径是有盘符开头的,有些系统是没有盘符的。
  
  相对路径: 相对路径是相对于当前程序的路径。当前路径就是执行java命令的时候,控制台所在的路径。
  
  类文件路径 :类文件路径就是使用了classpath的路径找对应的资源文件。
  
  如果需要使用到类文件路径首先先要获取到一个Class对象。

注意使用类文件路径要配置classpath的环境变量;set classpath=E:\我的java程序\eclips\javaLearn 注意不要加分号;

代码:

package com.java.fram;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/*
绝对路径:一个文件的完整路径信息。一般绝对路径是包含有盘符 的。 绝对路径的缺陷: 因为绝对路径是有盘符开头的,有些系统是没有盘符的。 相对路径: 相对路径是相对于当前程序的路径。当前路径就是执行java命令的时候,控制台所在的路径。 类文件路径 :类文件路径就是使用了classpath的路径找对应的资源文件。 如果需要使用到类文件路径首先先要获取到一个Class对象。
*/ public class PathExercise { static Properties properties; static{
//称为static代码块 ,也叫静态代码块,
//是在类中独立于类成员的static语句块,可以有多个,
//位置可以随便放,它不在任何的方法体内,JVM加载类时会执行这些静态的代码块,
//如果static代码块有多个,JVM将按照它们在类中出现的先后顺序依次执行它们,
//每个代码块只会被执行一次
try {
properties = new Properties(); Class clazz = PathExercise.class;
InputStream inputStream = clazz.getResourceAsStream("/db.user");
// "/"代表了Classpath的路径,getResourceAsStream使用了类文件路径
//properties.load(new FileReader("db.user"));
properties.load(inputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
System.out.println("当前路径: " + new File(".").getAbsolutePath());
System.out.println("用户名: " + properties.getProperty("userName") + "密码: " + properties.getProperty("passWord"));
} }

欢迎访问handsomecui的blog地址:

http://www.cnblogs.com/handsomecui/

网站:handsomecui.top