Source: https://juejin.cn/post/6844903907676651527

In Java, utility classes define a set of common methods, and this article introduces the most frequently used and common Java utility classes. The following tools and methods are ranked by popularity, with reference data from 50,000 open source projects randomly selected on Github.

A. Org.apache.com mons. IO. IOUtils

CloseQuietly: close an I/o flow, socket, or selector without raising an exception, usually in the finally block. ToString: converts I/o flow, Uri, byte[] toString copy: IO stream data is copied from the input stream to the output stream. A maximum of 2GB is supported. ToByteArray: obtains bytes from the input stream and URI. ToInputStream: Converts a character to an input streamreadLineIterator: returns an iterator from the input stream. It reads all the data required by the parameters. If there is not enough data, it failsCopy the code

2. Org.apache.com mons. IO. FileUtils

DeleteDirectory: deletes a folderreadFileToString: reads the contents of a file in the form of characters deleteQueitly: deletes a file or folder without throwing an exception copyFile: copies the file writeStringToFile: writes characters to the target file, and creates forceMkdir if the file does not exist: Write: writes characters to specified files. ListFiles: lists the files in a directory (according to the filter) copyDirectory: copies the folder forceDelete: deletes files forciblyCopy the code

3. Org.apache.com mons. Lang. StringUtils

IsBlank: whether the string isEmpty (check after trim) isEmpty: whether the string isEmpty (check after trim) equals: whether the string is equal join: merge the array into a single string and pass the delimiter split: split the string EMPTY: TrimToNull: Null string trimToNull: null replace: replace stringCopy the code

4. Org. Apache. HTTP. Util. EntityUtils

ToString: convert Entity toString consume: ensure that all contents of Entity are consumed. "ToByteArray", "Entity", "consumeQuietly", "toByteArray", "Entity", "consumeQuietly" As with consume, but without throwing exceptions getContentCharset: Get the encoding of the contentCopy the code

5. Org.apache.com mons. Lang3. StringUtils

IsBlank: whether the string isEmpty (check after trim) isEmpty: whether the string isEmpty (check after trim) equals: whether the string is equal join: merge the array into a single string and pass the delimiter split: split the string EMPTY: Returns the empty string replace: replaces the string capitalize: the first characterCopy the code

6. Org.apache.com mons. IO. FilenameUtils

GetBaseName: Returns the file name without the extension. GetName: returns the full name of the file. Concat: combines the file path in command line style. WildcardMatch: matches wildcards. SeperatorToUnix: separates paths. If the path is changed to Unix format, that is, / getFullPath: obtains the file path, excluding the file name. Check if the file suffix is one of the arguments passed in (List<String>)Copy the code

7. Org. Springframework. Util. StringUtils

HasText: check whether the string contains text hasLength: detecting whether string length is greater than 0 isEmpty: detecting whether the string is empty (if the incoming as object, the criterion to judge whether the object is null) commaDelimitedStringToArray: A comma to separate the String into an array of collectionToDelimitedString: the collection to CSV format String replace the replacement String 7. DelimitedListToStringArray: Equivalent to the split uncapitalize: first letters lowercase collectionToDelimitedCommaString: the collection to CSV format string tokenizeToStringArray: and the split is essentially the same, but can automatically remove empty wordsCopy the code

Eight. Org.apache.com mons. Lang. ArrayUtils

Contains: indicates whether it contains a string. AddAll: adds the entire arrayclone: clone an array isEmpty: whether an array isEmpty add: add an element to an array subarray: intercept an array indexOf: find the subscript of an element isEquals: compare the array to be equal toObject: convert an array of base-type data to the corresponding array of objectsCopy the code

9. Org.apache.com mons. Lang. StringEscapeUtils

Reference 15: org.apache.com mons. Lang3. StringEscapeUtilsCopy the code

Ten. Org. Apache. HTTP. Client. Utils. URLEncodedUtils

Application /x-www-form-urlencoded String Parse: Convert String or URI to List<NameValuePair>Copy the code

11. Org.apache.com mons. Codec. Digest. DigestUtils

Md5Hex: MD5 encryption, returns a 32-bit character string. Sha1Hex: SHA-1 encryption sha256Hex: SHA-256 encryption sha512Hex: SHA-512 encryption MD5: returns a 16-bit character string for MD5 encryptionCopy the code

12. Org.apache.com mons. Collections. CollectionUtils

Select: filters collection elements according to conditions transform: processes collection elements according to specified methods. Map () of a List filter: filters elements. Filter () of a Leyser List find: basically the same as select collect: It's almost the same as transform, but returns a new arrayforAllDo: calls the specified method for each element isEqualCollection: determines whether two collections are consistentCopy the code

13. Org.apache.com mons. Lang3. ArrayUtils

Contains: indicates whether it contains a string. AddAll: adds the entire arrayclone: clone an array isEmpty: whether an array isEmpty add: add an element to an array subarray: intercept an array indexOf: find the subscript of an element isEquals: compare the array to be equal toObject: convert an array of base-type data to the corresponding array of objectsCopy the code

14. Org.apache.com mons. Beanutils. PropertyUtils

GetProperty: Gets the object property valuesetGetPropertyDiscriptor: getPropertyDiscriptor: getPropertyDiscriptor: isReadable: Check whether the Property is accessible copyProperties: Copy property values from one object to another. GetPropertyDiscriptors: gets all property descriptors. IsWriteable: checks whether the property isWriteableCopy the code

15. Org.apache.com mons. Lang3. StringEscapeUtils

UnescapeHtml4: escape HTML escapeHtml4: escape HTML escapeXml: escapeXml escapeJava: escape unicode encoding escapeEcmaScript: Escape EcmaScript character unescapeJava: escape unicode encoding escapeJson: escapeJson character escapeXml10: escapeXml10Copy the code

This is now deprecated and we recommend using the method in the commons-text package.

16. Org.apache.com mons. Beanutils. Beanutils

CopyPeoperties: Copies property values from one object to another. GetProperty: Gets the property values of the objectsetProperty: Set the object Property values. Populate: copies the properties based on the Map. CopyPeoperty: copies a single value from one object to anothercloneBean: Clone the Bean instanceCopy the code

Now that you know the 16 most popular toolclass methods, you don’t have to write your own toolclass or reinvent the wheel. Most utility class methods are known by their name, but if you don’t know, look at how others use them or look up their usage online.

In addition, the tool class, according to Ali development manual, package name if you want to use util can not take S, the tool class named XxxUtils


Public id ↓↓↓