Java Apache Common-io Tutorial

Source: Internet
Author: User

Apache Common-io User Guide

User Guide

Commons-io contains utility Classes,endian classes,line iterator,file filters,file comparators and stream implementations.

If you would like more detailed description, please refer to Javadocs.

Utility Class Library

Ioutils

Ioutils contains practical methods for handling reading, writing, and copying. These methods work correctly on InputStream, OutputStream, reader, and writer.

For example, consider reading data from a URL and printing them. The standard way to use this is:

InputStream in =NewURL ("http://jakarta.apache.org"). OpenStream ();Try{InputStreamReader InR=NewInputStreamReader (in); BufferedReader buf=NewBufferedReader (InR);   String Line;  while(line = Buf.readline ())! =NULL) {System.out.println (line); }} finally{in.close ();} If you use the Ioutils class, you can do this: InputStream in=NewURL ("http://jakarta.apache.org"). OpenStream ();Try{System.out.println (ioutils.tostring (in));}finally{ioutils.closequietly (in);}

In some applications, their IO operations are common, and the use of this class can save a lot of time. You can rely entirely on proven code.

For the utility code above, flexibility and speed are the most important. But you should also understand the limitations of this approach. Reading 1GB files using these techniques will result in an attempt to create a 1GB string Object!

FileUtils

The FileUtils class provides some practical ways to manipulate file objects. Contains read, write, copy, and compare files.

For example, you can use the following method to read a whole file:

New File ("/commons/io/project.properties"); List lines= fileutils.readlines (file, "UTF-8"); Filenameutils

Filenameutils

The Filenameutils class contains methods that allow you to manipulate filenames without using file objects. The purpose of this class is to maintain the consistency of the program between UNIX and Windows to help with transitions between these environments, such as moving from development to production.

For example, standardize a file name to remove fragments of two adjacent points:

String filename = "c:/commons/io/." /lang/project.xml "= filenameutils.normalize (filename); // result is "C:/commons/lang/project.xml"

Filesystemutils

Filesystemutils contains some practical methods that the JDK does not provide to access the file system. Currently, only one method for reading the free space on the hard disk is available. Note that the command line is used instead of the local code.

Long freeSpace =filesystemutils.freespace ("c:/");

Endian class Library

Different computer architectures use different conventions for byte ordering. In so-called "low-priority" architectures (such as Intel), low-bit bytes are in the lowest in memory, and subsequent bytes are in higher positions. In a "high priority" architecture, such as Motorola, this is the opposite.

There are two related classes on this class library:

The endianutils contains a sequence of bytes used to exchange between the Java original object and the stream.

The Swappeddatainputstream class is an instance of the Datainput interface. With it, you can read a non-local byte sequence.

To learn more, see http://www.cs.umass.edu/~verts/cs32/endian.html

Line iterator

The Org.apache.commons.io.LineIterator class provides a flexible way to interact with line-based files. You can create an instance directly, or by using a factory method of Fileutils or Ioutils. The recommended usage modes are:

Lineiterator it = fileutils.lineiterator (file, "UTF-8"); Try {   while  (It.hasnext ()) {     = it.nextline ();      // /do something with line Finally {   lineiterator.closequietly (iterator);}     

File Filters

The Org.apache.commons.io.filefilter package defines an interface (Iofilefilter) that incorporates the Java.io.FileFilter and Java.io.FilenameFilter. In addition, this package also provides a series of directly available Iofilefilter implementation classes that can be combined with other file filters. For example, these file filters can be used when listing files or when using the file dialog box.

For more information, see FileFilter package Javadoc.

File comparators

The Org.apache.commons.io.comparator package provides some implementations of the Java.util.Comparator interface for Java.io.File. For example, you can use these comparators to sort a collection of files or arrays.

For more information, see Comparator package Javadoc.

Streams

The Org.apache.commons.io.input and Org.apache.commons.io.output packages contain a wide variety of implementations for data streams. Including:

Empty output stream-silently absorbing all the data sent to it

? T-output stream-all two output streams replace one for sending

? byte array output stream-this is a faster version of the JDK class

? Count Stream-Calculates the number of bytes passed

Proxy flow-Use the correct method to drag

Lock write-use locked files to provide synchronous writes

For more information, see Input or Output package Javadoc.

Reference: http://blog.csdn.net/wangyanan829/article/details/7843376

Java Apache Common-io Tutorial

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.