Java-BeanUtils的常用方法

435 阅读2分钟
Java-BeanUtils的常用方法

什么是BeanUtils工具
BeanUtils工具是一种方便我们对JavaBean进行操作的工具,是Apache组织下的产品。

BeanUtils工具一般可以方便javaBean的哪些操作?
1)beanUtils 可以便于对javaBean的属性进行赋值。
2)beanUtils 可以便于对javaBean的对象进行赋值。
3)beanUtils可以将一个MAP集合的数据拷贝到一个javabean对象中。

BeanUtils 类中的方法:
public static Map describe(Object bean)
英文解释:Return the entire set of properties for which thespecified bean provides a read method
用处:将java 对象中的属性以及值转换为map,注意返回的map中包含了类信息,如果不需要可以直接remove掉"class"
public static void populate(Object bean, Map properties)
英文解释:Populate the JavaBeans properties of the specifiedbean, based on the specified name/value pairs.
用处:把指定的键值对填充到指定的java 对象中。跟describe方法正好是反过来的。
public static String getNestedProperty(Object bean, String name)
英文解释:Return the value of the (possibly nested) propertyof the specified name, for the specified bean, as a String.
嵌套属性访问。使用这种方法,你将访问路径上的属性的名称用“.”拼接起来
例如getNestedProperty(test,"test1.test2.test3");通过这个方法能够直接获取test对象下test1属性下的test2属性下的test3属性
当然如果中间路径写错会报异常。
StringUtils.getFilename(filname) 获取文件名
注释:
Unix使用斜杆/ 作为路径分隔符,而web应用最新使用在Unix系统上面,所以目前所有的网络地址都采用 斜杆/ 作为分隔符。
Windows由于使用 斜杆/ 作为DOS命令提示符的参数标志了,为了不混淆,所以采用反斜杠\ 作为路径分隔符。所以目前windows系统上的文件浏览器都是用反斜杠\ 作为路径分隔符。随着发展,DOS系统已经被淘汰了,命令提示符也用的很少
大小写转换,空格不动:
StringUtils.swapCase(String str);
StringUtils.capitalize(String str); 首字母大写
StringUtils.uncapitalize(String str);首字母小写
StringUtils.isAlphanumericSpace(String str); 只由字母数字和空格组成
StringUtils.isAlphaspace( str); 如果str全由字母或空格组成返回True.
StringUtils.isNumeric( str); 如果str全由数字组成返回True.
StringUtils.isAlpha( str); 如果str全由字母组成返回True.
StringUtils.isNumeric( str); 如果str全由数字组成返回True.
StringUtils.isNumericSpace(String str); 只由数字和空格组成
StringUtils.abbreviate(str,width);
StringUtils.abbreviate(str,offset,width);
在给定的width内取得str的缩写,当testString的长度小于width则返回原字符串. 缩写的形式为…is a test…
字符串转数据或者判断字符串是否是数字常用工具类NumberUtils
NumberUtils.isNumber(String str) 判断是否为数字
NumberUtils.isDigits(String str) 判断是否是整数
SystemUtils----获取系统信息(原理都是调用System.getProperty())
SystemUtils.getJavaHome()
SystemUtils.getJavaIoTmpDir()
SystemUtils.getUserDir()
SystemUtils.getUserHome()
SystemUtils.JAVA_VERSION
SystemUtils.OS_NAME
SystemUtils.USER_TIMEZONE

更多技术资讯可扫描以下二维码获取