Home Java javaTutorial 16 most frequently used Java tool classes

16 most frequently used Java tool classes

Nov 11, 2019 pm 05:45 PM
java Tools

16 most frequently used Java tool classes

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

1

2

3

4

5

6

7

8

9

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:从输入流返回一个迭代器,根据参数要求读取的数据量,全部读取,如果数据不够,则失败

Copy after login

2. org.apache.commons.io.FileUtils

1

2

3

4

5

6

7

8

9

10

deleteDirectory:删除文件夹

readFileToString:以字符形式读取文件内容

deleteQueitly:删除文件或文件夹且不会抛出异常

copyFile:复制文件

writeStringToFile:把字符写到目标文件,如果文件不存在,则创建

forceMkdir:强制创建文件夹,如果该文件夹父级目录不存在,则创建父级

write:把字符写到指定文件中

listFiles:列举某个目录下的文件(根据过滤器)

copyDirectory:复制文件夹

forceDelete:强制删除文件

Copy after login

三、org.apache.commons.lang.StringUtils

1

2

3

4

5

6

7

8

isBlank:字符串是否为空 (trim后判断)

isEmpty:字符串是否为空 (不trim并判断)

equals:字符串是否相等

join:合并数组为单一字符串,可传分隔符

split:分割字符串

EMPTY:返回空字符串

trimToNull:trim后为空字符串则转换为null

replace:替换字符串

Copy after login

四、org.apache.http.util.EntityUtils

1

2

3

4

5

6

toString:把Entity转换为字符串

consume:确保Entity中的内容全部被消费。可以看到源码里又一次消费了Entity的内容,假如用户没有消费,那调用Entity

时候将会把它消费掉

toByteArray:把Entity转换为字节流

consumeQuietly:和consume一样,但不抛异常

getContentCharset:获取内容的编码

Copy after login

5. org.apache.commons.lang3.StringUtils

1

2

3

4

5

6

7

8

isBlank:字符串是否为空 (trim后判断)

isEmpty:字符串是否为空 (不trim并判断)

equals:字符串是否相等

join:合并数组为单一字符串,可传分隔符

split:分割字符串

EMPTY:返回空字符串

replace:替换字符串

capitalize:首字符大写

Copy after login

6. org.apache.commons.io.FilenameUtils

1

2

3

4

5

6

7

8

9

10

getExtension:返回文件后缀名

getBaseName:返回文件名,不包含后缀名

getName:返回文件全名

concat:按命令行风格组合文件路径(详见方法注释)

removeExtension:删除后缀名

normalize:使路径正常化

wildcardMatch:匹配通配符

seperatorToUnix:路径分隔符改成unix系统格式的,即/

getFullPath:获取文件路径,不包括文件名

isExtension:检查文件后缀名是不是传入参数(List<String>)中的一个

Copy after login

Seven, org.springframework.util.StringUtils

1

2

3

4

5

6

7

8

9

10

hasText:检查字符串中是否包含文本

hasLength:检测字符串是否长度大于0

isEmpty:检测字符串是否为空(若传入为对象,则判断对象是否为null)

commaDelimitedStringToArray:逗号分隔的String转换为数组

collectionToDelimitedString:把集合转为CSV格式字符串

replace 替换字符串

delimitedListToStringArray:相当于split

uncapitalize:首字母小写

collectionToDelimitedCommaString:把集合转为CSV格式字符串

tokenizeToStringArray:和split基本一样,但能自动去掉空白的单词

Copy after login

Eight, org.apache.commons.lang.ArrayUtils

1

2

3

4

5

6

7

8

9

contains:是否包含某字符串

addAll:添加整个数组

clone:克隆一个数组

isEmpty:是否空数组

add:向数组添加元素

subarray:截取数组

indexOf:查找某个元素的下标

isEquals:比较数组是否相等

toObject:基础类型数据数组转换为对应的Object数组

Copy after login

Nine , org.apache.commons.lang.StringEscapeUtils

1

参考十五:org.apache.commons.lang3.StringEscapeUtils

Copy after login

十、org.apache.http.client.utils.URLEncodedUtils

1

2

format:格式化参数,返回一个HTTP POST或者HTTP PUT可用application/x-www-form-urlencoded字符串

parse:把String或者URI等转换为List<NameValuePair>

Copy after login

十1. org.apache.commons.codec.digest.DigestUtils

1

2

3

4

5

md5Hex:MD5加密,返回32位字符串

sha1Hex:SHA-1加密

sha256Hex:SHA-256加密

sha512Hex:SHA-512加密

md5:MD5加密,返回16位字符串

Copy after login

12. org.apache.commons.collections.CollectionUtils

1

2

3

4

5

6

7

8

isEmpty:是否为空

select:根据条件筛选集合元素

transform:根据指定方法处理集合元素,类似List的map()

filter:过滤元素,雷瑟List的filter()

find:基本和select一样

collect:和transform 差不多一样,但是返回新数组

forAllDo:调用每个元素的指定方法

isEqualCollection:判断两个集合是否一致

Copy after login

Thirteen, org.apache.commons.lang3.ArrayUtils

1

2

3

4

5

6

7

8

9

contains:是否包含某个字符串

addAll:添加整个数组

clone:克隆一个数组

isEmpty:是否空数组

add:向数组添加元素

subarray:截取数组

indexOf:查找某个元素的下标

isEquals:比较数组是否相等

toObject:基础类型数据数组转换为对应的Object数组

Copy after login

Fourteen, org.apache.commons.beanutils.PropertyUtils

1

2

3

4

5

6

7

8

getProperty:获取对象属性值

setProperty:设置对象属性值

getPropertyDiscriptor:获取属性描述器

isReadable:检查属性是否可访问

copyProperties:复制属性值,从一个对象到另一个对象

getPropertyDiscriptors:获取所有属性描述器

isWriteable:检查属性是否可写

getPropertyType:获取对象属性类型

Copy after login

15. org.apache.commons.lang3.StringEscapeUtils

1

2

3

4

5

6

7

8

9

unescapeHtml4:转义html

escapeHtml4:反转义html

escapeXml:转义xml

unescapeXml:反转义xml

escapeJava:转义unicode编码

escapeEcmaScript:转义EcmaScript字符

unescapeJava:反转义unicode编码

escapeJson:转义json字符

escapeXml10:转义Xml10

Copy after login

This is now obsolete. It is recommended to use the method in the commons-text package.

16. org.apache.commons.beanutils.BeanUtils

1

2

3

4

5

6

copyPeoperties:复制属性值,从一个对象到另一个对象

getProperty:获取对象属性值

setProperty:设置对象属性值

populate:根据Map给属性复制

copyPeoperty:复制单个值,从一个对象到另一个对象

cloneBean:克隆bean实例

Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

TimeStamp to Date in Java TimeStamp to Date in Java Aug 30, 2024 pm 04:28 PM

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Java Program to Find the Volume of Capsule Java Program to Find the Volume of Capsule Feb 07, 2025 am 11:37 AM

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Create the Future: Java Programming for Absolute Beginners Create the Future: Java Programming for Absolute Beginners Oct 13, 2024 pm 01:32 PM

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

See all articles