Top 16 Java Tool classes

Source: Internet
Author: User

In Java, the tool class defines a common set of methods that describe the most frequently used and most common Java tool classes in Java. The following tool classes, methods by popularity rankings, reference data from GitHub on a randomly selected 50,000 open source project source.

I. Org.apache.commons.io.IOUtils
closeQuietly:关闭一个IO流、socket、或者selector且不抛出异常,通常放在finally块toString:转换IO流、 Uri、 byte[]为Stringcopy:IO流数据复制,从输入流写到输出流中,最大支持2GBtoByteArray:从输入流、URI获取byte[]write:把字节. 字符等写入输出流toInputStream:把字符转换为输入流readLines:从输入流中读取多行数据,返回List<String>copyLarge:同copy,支持2GB以上数据的复制lineIterator:从输入流返回一个迭代器,根据参数要求读取的数据量,全部读取,如果数据不够,则失败
Two. Org.apache.commons.io.FileUtils
deleteDirectory:删除文件夹readFileToString:以字符形式读取文件内容deleteQueitly:删除文件或文件夹且不会抛出异常copyFile:复制文件writeStringToFile:把字符写到目标文件,如果文件不存在,则创建forceMkdir:强制创建文件夹,如果该文件夹父级目录不存在,则创建父级write:把字符写到指定文件中listFiles:列举某个目录下的文件(根据过滤器)copyDirectory:复制文件夹forceDelete:强制删除文件
Three. Org.apache.commons.lang.StringUtils
isBlank:字符串是否为空 (trim后判断)isEmpty:字符串是否为空 (不trim并判断)equals:字符串是否相等join:合并数组为单一字符串,可传分隔符split:分割字符串EMPTY:返回空字符串trimToNull:trim后为空字符串则转换为nullreplace:替换字符串
Four. Org.apache.http.util.EntityUtils
toString:把Entity转换为字符串consume:确保Entity中的内容全部被消费。可以看到源码里又一次消费了Entity的内容,假如用户没有消费,那调用Entity时候将会把它消费掉toByteArray:把Entity转换为字节流consumeQuietly:和consume一样,但不抛异常getContentCharset:获取内容的编码
Five. Org.apache.commons.lang3.StringUtils
isBlank:字符串是否为空 (trim后判断)isEmpty:字符串是否为空 (不trim并判断)equals:字符串是否相等join:合并数组为单一字符串,可传分隔符split:分割字符串EMPTY:返回空字符串replace:替换字符串capitalize:首字符大写
Six. Org.apache.commons.io.FilenameUtils
getExtension:返回文件后缀名getBaseName:返回文件名,不包含后缀名getName:返回文件全名concat:按命令行风格组合文件路径(详见方法注释)removeExtension:删除后缀名normalize:使路径正常化wildcardMatch:匹配通配符seperatorToUnix:路径分隔符改成unix系统格式的,即/getFullPath:获取文件路径,不包括文件名isExtension:检查文件后缀名是不是传入参数(List<String>)中的一个
Seven. Org.springframework.util.StringUtils
hasText:检查字符串中是否包含文本hasLength:检测字符串是否长度大于0isEmpty:检测字符串是否为空(若传入为对象,则判断对象是否为null)commaDelimitedStringToArray:逗号分隔的String转换为数组collectionToDelimitedString:把集合转为CSV格式字符串replace 替换字符串7. delimitedListToStringArray:相当于splituncapitalize:首字母小写collectionToDelimitedCommaString:把集合转为CSV格式字符串tokenizeToStringArray:和split基本一样,但能自动去掉空白的单词
Eight. Org.apache.commons.lang.ArrayUtils
contains:是否包含某字符串addAll:添加整个数组clone:克隆一个数组isEmpty:是否空数组add:向数组添加元素subarray:截取数组indexOf:查找某个元素的下标isEquals:比较数组是否相等toObject:基础类型数据数组转换为对应的Object数组
Nine. Org.apache.commons.lang.StringEscapeUtils
参考十五:org.apache.commons.lang3.StringEscapeUtils
10. Org.apache.http.client.utils.URLEncodedUtils
format:格式化参数,返回一个HTTP POST或者HTTP PUT可用application/x-www-form-urlencoded字符串parse:把String或者URI等转换为List<NameValuePair>
Eleven. Org.apache.commons.codec.digest.DigestUtils
md5Hex:MD5加密,返回32位字符串sha1Hex:SHA-1加密sha256Hex:SHA-256加密sha512Hex:SHA-512加密md5:MD5加密,返回16位字符串
Twelve. Org.apache.commons.collections.CollectionUtils
isEmpty:是否为空select:根据条件筛选集合元素transform:根据指定方法处理集合元素,类似List的map()filter:过滤元素,雷瑟List的filter()find:基本和select一样collect:和transform 差不多一样,但是返回新数组forAllDo:调用每个元素的指定方法isEqualCollection:判断两个集合是否一致
13. Org.apache.commons.lang3.ArrayUtils
contains:是否包含某个字符串addAll:添加整个数组clone:克隆一个数组isEmpty:是否空数组add:向数组添加元素subarray:截取数组indexOf:查找某个元素的下标isEquals:比较数组是否相等toObject:基础类型数据数组转换为对应的Object数组
14. Org.apache.commons.beanutils.PropertyUtils
getProperty:获取对象属性值setProperty:设置对象属性值getPropertyDiscriptor:获取属性描述器isReadable:检查属性是否可访问copyProperties:复制属性值,从一个对象到另一个对象getPropertyDiscriptors:获取所有属性描述器isWriteable:检查属性是否可写getPropertyType:获取对象属性类型
xv. Org.apache.commons.lang3.StringEscapeUtils
unescapeHtml4:转义htmlescapeHtml4:反转义htmlescapeXml:转义xmlunescapeXml:反转义xmlescapeJava:转义unicode编码escapeEcmaScript:转义EcmaScript字符unescapeJava:反转义unicode编码escapeJson:转义json字符escapeXml10:转义Xml10

This is now obsolete, it is recommended to use the Commons-text package inside the method.

16. Org.apache.commons.beanutils.BeanUtils
copyPeoperties:复制属性值,从一个对象到另一个对象getProperty:获取对象属性值setProperty:设置对象属性值populate:根据Map给属性复制copyPeoperty:复制单个值,从一个对象到另一个对象cloneBean:克隆bean实例

Now that you know all 16 of the most popular tools, you don't have to write your own tool class again, you don't have to reinvent the wheel. Most tool class methods can understand their use by their name, if not clear, can look at how others are used, or go online to inquire about their usage.

In addition, the tool class, according to the Ali Development Manual, the package name if you want to use Util can not take s, the tool class named XxxUtils .

Top 16 Java Tool classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.