了解java的这些功能可以让你少走很多弯路

平常工作中的一些现成的方法可以减少我们很多的工作时间,下面只是我总结的一小部分常用的类和方法,我会接着发~

文末有余胜军老师的学习视频连接大家感兴趣的可以下载下来看看


BeanUtils 类中的方法:

public static Map describe(Object bean)

英文解释:Return the entire set of properties for which the specified bean provides a read method

用处:将java 对象中的属性以及值转换为map,注意返回的map中包含了类信息,如果不需要可以直接remove掉"class"


public static void populate(Object bean, Map properties)

英文解释:Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.

用处:把指定的键值对填充到指定的java 对象中。跟describe方法正好是反过来的。


public static String getNestedProperty(Object bean, String name)

英文解释:Return the value of the (possibly nested) property of 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


猜你喜欢

转载自blog.51cto.com/15023245/2647475