Commons Io 简明教程
Apache Commons IO - Overview
Apache Commons IO 库为文件 IO 的常见操作提供了各种实用程序类,涵盖了广泛的用例。它有助于避免编写样板代码。
Apache Commons IO library provides various utility classes for common operations for File IO covering wide range of use cases. It helps avoid writing boilerplate code.
Classes
Apache Commons IO 库为以下类别提供类 −
Apache Commons IO library provides classes for following categories −
Utility classes
这些 org.apache.commons.io 包内的类提供了文件和字符串比较。以下是一些示例。
These classes which are under org.apache.commons.io package, provides file and string comparison. Following are some of the examples.
-
IOUtils − Provides utility methods for reading, writing and copying files. The methods work with InputStream, OutputStream, Reader and Writer.
-
FilenameUtils − Provides method to work with file names without using File Object. It works on different operating systems in similar way.
-
FileUtils − Provides method to manipulates files like moving, opening, checking existence, reading of file etc. These methods use File Object.
-
IOCase − Provides method for string manipulation and comparison.
-
FileSystemUtils − Provides method to get the free space on a disk drive.
-
LineIterator − Provides a flexible way to work with a line-based file.
Filter classes
org.apache.commons.io.filefilter 包内的过滤器类提供了基于逻辑条件而不是基于字符串的繁琐比较来筛选文件的方法。以下是一些示例。
Filter classes which are under org.apache.commons.io.filefilter package, provides methods to filter files based on logical criteria instead of string based tedious comparisons. Following are some of the examples.
-
NameFileFilter − Filters file-names for a name.
-
WildcardFileFilter − Filters files using the supplied wildcards.
-
SuffixFileFilter − Filters files based on suffix. This is used in retrieving all the files of a particular type.
-
PrefixFileFilter − Filters files based on prefix.
-
OrFileFilter − Provides conditional OR logic across a list of file filters. Returns true, if any filters in the list return true. Otherwise, it returns false.
-
AndFileFilter − Provides conditional and logic across a list of file filters. Returns false if any filters in the list return false. Otherwise, it returns true.
File Monitor classes
在 org.apache.commons.io.monitor 包中的文件监视器类,提供了跟踪特定文件或文件夹中的更改以及允许对更改进行相应操作的控制。以下是一些示例。
File monitor classes which are under org.apache.commons.io.monitor package, provides control to track changes in a specific file or folder and allows to do action accordingly on the changes. Following are some of the examples.
-
FileEntry − Provides the state of a file or directory. File attributes at a point in time.
-
FileAlterationObserver − Represents the state of files below a root directory, checks the file system and notifies listeners of create, change or delete events.
-
FileAlterationMonitor − Represents a thread that spawns a monitoring thread triggering any registered FileAlterationObserver at a specified interval.
Comparator classes
org.apache.commons.io.comparator 包中的文件监视器类允许轻松比较和分类文件和目录。
File monitor classes under org.apache.commons.io.comparator package allow to compare and sort files and directories easily.
-
NameFileComparator − Compare the names of two files.
-
SizeFileComparator − Compare the size of two files.
-
LastModifiedFileComparator − Compare the last modified dates of two files.
Stream classes
在 org.apache.commons.io.input 包中有多个 InputStream 实现,在 org.apache.commons.io.output 包中有 OutputStream 实现,以对流执行有用的任务。以下是一些示例。
There are multiple implementation of InputStream under org.apache.commons.io.input package and of OutputStream under org.apache.commons.io.output package, to do useful tasks on streams. Following are some of the examples.
-
NullOutputStream − Absorbs all data sent with any error.
-
TeeOutputStream − Sends output to two streams.
-
ByteArrayOutputStream − Faster version of JDK class.
-
CountingOutputStream − Counts the number of bytes passed through the stream.
-
ProxyOutputStream − Changes the calls to proxy stream.
-
LockableFileWriter − A FileWriter to create lock files and allow simple cross thread file lock handling.