In Java, tool classes define a set of public methods. This article will introduce the most frequently used and versatile Java tool classes in Java. The following tools and methods are ranked by usage popularity. The reference data comes from 50,000 randomly selected open source project source codes on Github.
1. org.apache.commons.io.IOUtils
closeQuietly:关闭一个IO流、socket、或者selector且不抛出异常,通常放在finally块 toString:转换IO流、 Uri、 byte[]为String copy:IO流数据复制,从输入流写到输出流中,最大支持2GB toByteArray:从输入流、URI获取byte[] write:把字节. 字符等写入输出流 toInputStream:把字符转换为输入流 readLines:从输入流中读取多行数据,返回List<String> copyLarge:同copy,支持2GB以上数据的复制 lineIterator:从输入流返回一个迭代器,根据参数要求读取的数据量,全部读取,如果数据不够,则失败
2. org.apache.commons.io.FileUtils
deleteDirectory:删除文件夹 readFileToString:以字符形式读取文件内容 deleteQueitly:删除文件或文件夹且不会抛出异常 copyFile:复制文件 writeStringToFile:把字符写到目标文件,如果文件不存在,则创建 forceMkdir:强制创建文件夹,如果该文件夹父级目录不存在,则创建父级 write:把字符写到指定文件中 listFiles:列举某个目录下的文件(根据过滤器) copyDirectory:复制文件夹 forceDelete:强制删除文件
三、org.apache.commons.lang.StringUtils
isBlank:字符串是否为空 (trim后判断) isEmpty:字符串是否为空 (不trim并判断) equals:字符串是否相等 join:合并数组为单一字符串,可传分隔符 split:分割字符串 EMPTY:返回空字符串 trimToNull:trim后为空字符串则转换为null replace:替换字符串
四、org.apache.http.util.EntityUtils
toString:把Entity转换为字符串 consume:确保Entity中的内容全部被消费。可以看到源码里又一次消费了Entity的内容,假如用户没有消费,那调用Entity 时候将会把它消费掉 toByteArray:把Entity转换为字节流 consumeQuietly:和consume一样,但不抛异常 getContentCharset:获取内容的编码
5. org.apache.commons.lang3.StringUtils
isBlank:字符串是否为空 (trim后判断) isEmpty:字符串是否为空 (不trim并判断) equals:字符串是否相等 join:合并数组为单一字符串,可传分隔符 split:分割字符串 EMPTY:返回空字符串 replace:替换字符串 capitalize:首字符大写
6. 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:检测字符串是否长度大于0 isEmpty:检测字符串是否为空(若传入为对象,则判断对象是否为null) commaDelimitedStringToArray:逗号分隔的String转换为数组 collectionToDelimitedString:把集合转为CSV格式字符串 replace 替换字符串 delimitedListToStringArray:相当于split uncapitalize:首字母小写 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
十、org.apache.http.client.utils.URLEncodedUtils
format:格式化参数,返回一个HTTP POST或者HTTP PUT可用application/x-www-form-urlencoded字符串 parse:把String或者URI等转换为List<NameValuePair>
十1. org.apache.commons.codec.digest.DigestUtils
md5Hex:MD5加密,返回32位字符串 sha1Hex:SHA-1加密 sha256Hex:SHA-256加密 sha512Hex:SHA-512加密 md5:MD5加密,返回16位字符串
12. org.apache.commons.collections.CollectionUtils
isEmpty:是否为空 select:根据条件筛选集合元素 transform:根据指定方法处理集合元素,类似List的map() filter:过滤元素,雷瑟List的filter() find:基本和select一样 collect:和transform 差不多一样,但是返回新数组 forAllDo:调用每个元素的指定方法 isEqualCollection:判断两个集合是否一致
Thirteen, org.apache.commons.lang3.ArrayUtils
contains:是否包含某个字符串 addAll:添加整个数组 clone:克隆一个数组 isEmpty:是否空数组 add:向数组添加元素 subarray:截取数组 indexOf:查找某个元素的下标 isEquals:比较数组是否相等 toObject:基础类型数据数组转换为对应的Object数组
Fourteen, org.apache.commons.beanutils.PropertyUtils
getProperty:获取对象属性值 setProperty:设置对象属性值 getPropertyDiscriptor:获取属性描述器 isReadable:检查属性是否可访问 copyProperties:复制属性值,从一个对象到另一个对象 getPropertyDiscriptors:获取所有属性描述器 isWriteable:检查属性是否可写 getPropertyType:获取对象属性类型
15. org.apache.commons.lang3.StringEscapeUtils
unescapeHtml4:转义html escapeHtml4:反转义html escapeXml:转义xml unescapeXml:反转义xml escapeJava:转义unicode编码 escapeEcmaScript:转义EcmaScript字符 unescapeJava:反转义unicode编码 escapeJson:转义json字符 escapeXml10:转义Xml10
This is now obsolete. It is recommended to use the method in the commons-text package.
16. org.apache.commons.beanutils.BeanUtils
copyPeoperties:复制属性值,从一个对象到另一个对象 getProperty:获取对象属性值 setProperty:设置对象属性值 populate:根据Map给属性复制 copyPeoperty:复制单个值,从一个对象到另一个对象 cloneBean:克隆bean实例
Now as long as you understand the above 16 most popular tool methods, you don’t need to You no longer need to write your own tool classes, so you don’t have to reinvent the wheel. Most tool methods can be understood through their names. If you are not sure, you can look at how others use them, or check their usage online.
In addition: Tool class, according to the Alibaba development manual, if you want to use util, the package name cannot contain s, and the tool class is named XxxUtils.
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of 16 most frequently used Java tool classes. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.