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.