Apache Commons Library: I agree when I use it!

Source: Internet
Author: User
Document directory
  • 1. commons-io
  • 2. commons-cli
  • 3. commons-Lang
  • 4. commons-Configuration

Recently I wrote a program and found that the Apache commons (http://commons.apache.org/) Library is really good, can simplify a lot of tedious tasks in Java, helped us create a lot of wheels.

1. commons-io

I/O-related tool methods.

 

Example 1: Disable a stream

Bufferedwriter out = NULL; <br/> try {<br/> out = new bufferedwriter (...); <br/> out. write (...); <br/>... <br/>}< br/> catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> finally {<br/> If (OUT! = NULL) {<br/> try {out. Close () ;}catch (ioexception e) {}< br/>}< br/>}

In Finally, the general method of closing the stream is used. You only need to write ioutils as follows:

Ioutils. closequietly (out );

 

Example 2: read an object

List <string> = fileutils. readlines (File); // read the file content in the list by row. <br/> byte [] = fileutils. readfiletobytearray (File); // You can also read the byte array.

It can also be read directly from inputstream.

 

Example 3: traverse a directory

 

Commons-io provides directorywalker for Recursive directory traversal. Your program only needs to inherit directorywalker and then implement some methods. "Directory loader", the name is cool

2. commons-cli

Commons-CLI can help you parse command line parameters.

 

All you need to write is code like this:

// Options is used to hold all parameters. <br/> options Options = new options (); </P> <p> // Add parameters, specify parameter attributes <br/> options. addoption ("T", false, "display current time"); </P> <p> // resolution parameter <br/> commandlineparser parser = new posixparser (); <br/> CommandLine cmd = parser. parse (options, argS); </P> <p> // read the parsing result <br/> If (CMD. hasoption ("T") {<br/>... <br/>}< br/> else {<br/>... <br/>}

Helpformatter also helps you automatically generate a command line option, which is very considerate.

3. commons-Lang

Provides many basic tools and methods to enhance the classes in the Java. lang package. String processing is usually used, that is, stringutils and stringescapeutils.

I used stringescapeutils. escapehtml (string): to replace the special HTML characters (<> ", etc.) in the string with HTML entities.

 

Also, strtokenizer can perform more complex Syntax Parsing than stringtokenizer.

 

4. commons-Configuration

No, but it looks good. Provides a unified interface to read configurations from various places, such as properties files, Windows INI files, XML documents, and applet/servlet parameters.

PS. In addition, commons-Pool provides a general object pool implementation, which I will introduce in another article.

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.