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

 
Copy the code
  1. CloseQuietly: Close an I/O flow, socket, or selector without throwing an exception, usually in the finally block
  2. ToString: converts IO stream, Uri, and byte[] toString
  3. Copy: copy data from the input stream to the output stream. A maximum of 2GB is supported
  4. ToByteArray: byte from input stream, URI []
  5. Write: write bytes. Characters and so on are written to the output stream
  6. ToInputStream: Converts characters to an input stream
  7. ReadLines: Reads multiple lines from the input stream, returning List<String>
  8. CopyLarge: same as copy, supports the replication of more than 2GB data
  9. LineIterator: Returns an iterator from the input stream that reads as much data as required by the argument, and fails if there is not enough data

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

 
Copy the code
  1. DeleteDirectory: deletes a folder
  2. ReadFileToString: Reads file contents as characters
  3. DeleteQueitly: Deletes a file or folder without throwing an exception
  4. CopyFile: Copies a file
  5. WriteStringToFile: Writes characters to the target file or creates them if the file does not exist
  6. ForceMkdir: creates a folder forcibly. If the parent directory of the folder does not exist, the parent directory is created
  7. Write: Writes characters to the specified file
  8. ListFiles: Lists the files in a directory (according to the filter)
  9. CopyDirectory: copy folder
  10. ForceDelete: forcibly deletes a file

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

 
Copy the code
  1. IsBlank: indicates whether the string is empty.
  2. IsEmpty: whether the string isEmpty (not trim and check)
  3. Equals: Whether the strings are equal
  4. Join: Merges arrays into a single string, which can be delimited
  5. Split: splits the string
  6. EMPTY: Returns an EMPTY string
  7. TrimToNull: Converts to null if trim is null
  8. Replace: replaces the string

4. Org. Apache. HTTP. Util. EntityUtils

 
Copy the code
  1. ToString: Converts an Entity to a string
  2. Consume: Ensure that all the contents of an Entity are consumed. You can see that the source consumes the content of Entity again. If the user does not consume Entity, it will be consumed when calling Entity
  3. ToByteArray: Converts an Entity to a byte stream
  4. ConsumeQuietly: As consume, but do not fly
  5. GetContentCharset: Gets the encoding of the content

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

 
Copy the code
  1. IsBlank: indicates whether the string is empty.
  2. IsEmpty: whether the string isEmpty (not trim and check)
  3. Equals: Whether the strings are equal
  4. Join: Merges arrays into a single string, which can be delimited
  5. Split: splits the string
  6. EMPTY: Returns an EMPTY string
  7. Replace: replaces the string
  8. Capitalize: Capitalize the first character

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

 
Copy the code
  1. GetExtension: Returns the file extension name
  2. GetBaseName: Returns the file name, excluding the suffix
  3. GetName: Returns the full name of the file
  4. Concat: combine file paths in command line style (see method comments)
  5. RemoveExtension: Delete the extension name
  6. Normalize: Normalizes a path
  7. WildcardMatch: matches the wildcard
  8. SeperatorToUnix: The path separator is changed to Unix format, that is, /
  9. GetFullPath: obtains the file path, excluding the file name
  10. IsExtension: Checks whether the file suffix is one of the passed arguments (List<String>)

7. Org. Springframework. Util. StringUtils

 
Copy the code
  1. HasText: Checks if the string contains text
  2. HasLength: checks whether the string length is greater than 0
  3. IsEmpty: checks if the string isEmpty (null if passed in as an object)
  4. CommaDelimitedStringToArray: a comma to separate the String into an array
  5. CollectionToDelimitedString: the collection to CSV format string
  6. Replace Replace string
  7. 7. DelimitedListToStringArray: equivalent to the split
  8. Uncapitalize: Lowercase first letter
  9. CollectionToDelimitedCommaString: the collection to CSV format string
  10. TokenizeToStringArray: Basically the same as split, but automatically removes blank words

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

 
Copy the code
  1. Contains: indicates whether it contains a string
  2. AddAll: Adds the entire array
  3. Clone: Clones an array
  4. IsEmpty: indicates whether the array isEmpty
  5. Add: Adds elements to an array
  6. Subarray: intercepts an array
  7. IndexOf: finds the indexOf an element
  8. IsEquals: Compares whether an array is equal
  9. ToObject: An array of data of the underlying type is converted to the corresponding Object array

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

 
Copy the code
  1. Reference 15: org.apache.com mons. Lang3. StringEscapeUtils

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

 
Copy the code
  1. Format: Format parameter, return an HTTP POST or HTTP PUT application/ X-www-form-urlencoded string
  2. Parse: Convert String or URI to List<NameValuePair>

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

 
Copy the code
  1. Md5Hex: returns a 32-bit character string in MD5 encryption
  2. Sha1Hex: sha-1 encryption
  3. Sha256Hex: sha-256 encryption
  4. Sha512Hex: sha-512 encryption
  5. Md5: returns a 16-bit string in MD5 encryption

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

 
Copy the code
  1. IsEmpty: indicates whether the value isEmpty
  2. Select: Filters collection elements based on criteria
  3. Transform: Handles collection elements according to the specified method, like a List map()
  4. Filter: Filter element, filter() of List
  5. Find: Basically the same as select
  6. Collect: Much the same as transform, but returns a new array
  7. ForAllDo: Calls the specified method for each element
  8. IsEqualCollection: Determines whether two collections are consistent

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

 
Copy the code
  1. Contains: Indicates whether it contains a string
  2. AddAll: Adds the entire array
  3. Clone: Clones an array
  4. IsEmpty: indicates whether the array isEmpty
  5. Add: Adds elements to an array
  6. Subarray: intercepts an array
  7. IndexOf: finds the indexOf an element
  8. IsEquals: Compares whether an array is equal
  9. ToObject: An array of data of the underlying type is converted to the corresponding Object array

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

 
Copy the code
  1. GetProperty: Gets the object property value
  2. SetProperty: Sets the value of an object property
  3. GetPropertyDiscriptor: Gets the property descriptor
  4. IsReadable: Checks whether the attribute is accessible
  5. CopyProperties: Copies property values from one object to another
  6. GetPropertyDiscriptors: Gets all property descriptors
  7. IsWriteable: checks whether the attribute isWriteable
  8. GetPropertyType: Gets the object property type

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

 
Copy the code
  1. UnescapeHtml4: escapes HTML
  2. EscapeHtml4: Reverse escape HTML
  3. EscapeXml: Escapes XML
  4. UnescapeXml: reverse escape XML
  5. EscapeJava: Escapes Unicode encoding
  6. EscapeEcmaScript: Escapes EcmaScript characters
  7. UnescapeJava: UnescapeUnicode encoding
  8. EscapeJson: Escapes JSON characters
  9. EscapeXml10: Escapes Xml10

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

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

 
Copy the code
  1. CopyPeoperties: Copies property values from one object to another
  2. GetProperty: Gets the object property value
  3. SetProperty: Sets the value of an object property
  4. Populate: Copies properties based on the Map
  5. CopyPeoperty: Copies a single value, from one object to another
  6. CloneBean: cloneBean instance

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, if the package name should use util without S, the tool class is named XxxUtils.

This article was originally published in wechat public account: Java Technology Stack (ID: Javastack), pay attention to the public account in the background reply “tools” can get more Java development tools tutorial, reprint please retain this information.