(java 往日的翻译) commons-io user guide

访问量: 687

User guide 使用指南
Commons-IO contains utility classes , endian classes , line iterator , file filters , file comparators and stream implementations .
Commons-IO 包含了工具类,endian类,line iterator, file filter(文件过滤器),file comparator(文件比较器)和stream 实现。

For a more detailed descriptions, take a look at the javadocs . 更多细节请看javadoc.
Utility classes 工具类
IOUtils contains utility methods dealing with reading, writing and copying. The methods work on InputStream, OutputStream, Reader and Writer.
IOUtils包含了可以处理读,写和copy的工具方法。该方法运行于InputStream, OutputStream, Reader和Writer的基础之上。
As an example, consider the task of reading bytes from a URL, and printing them. This would typically done like this:
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
InputStreamReader inR = new InputStreamReader( in );
BufferedReader buf = new BufferedReader( inR );
String line;
while ( ( line = buf.readLine() ) != null ) {
System.out.println( line );
} finally {
With the IOUtils class, that could be done with:
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
System.out.println( IOUtils.toString( in ) );
} finally {
In certain application domains, such IO operations are common, and this class can save a great deal of time. And you can rely on well-tested code.
For utility code such as this, flexibility and speed are of primary importance. However you should also understand the limitations of this approach. Using the above technique to read a 1GB file would result in an attempt to create a 1GB String object!

The FileUtils class contains utility methods for working with File objects. These include reading, writing, copying and comparing files.
FileUtils类包含了对操作文件对象的工具方法。这些method包括:读,写, COPY和比较文件。
For example to read an entire file line by line you could use:
File file = new File("/commons/io/project.properties");
List lines = FileUtils.readLines(file, "UTF-8");
The FilenameUtils class contains utility methods for working with filenames without using File objects. The class aims to be consistent between Unix and Windows, to aid transitions between these environments (such as moving from development to production).
For example to normalize a filename removing double dot segments:
String filename = "C:/commons/io/../lang/project.xml";
String normalized = FilenameUtils.normalize(filename);
// result is "C:/commons/lang/project.xml"
The FileSystemUtils class contains utility methods for working with the file system to access functionality not supported by the JDK. Currently, the only method is to get the free space on a drive. Note that this uses the command line, not native code.
FileSystemUtils类包含了操作文件系统的JDK没有提供的方法。目前为止,唯一的method是取得某个驱动器上的剩余空间。注意:该方法使用的是命令行,而不是native code(原生代码?).

For example to find the free space on a drive:
long freeSpace = FileSystemUtils.freeSpace("C:/");
Endian classes (译注: Endian是啥?)
Different computer architectures adopt different conventions for byte ordering. In so-called "Little Endian" architectures (eg Intel), the low-order byte is stored in memory at the lowest address, and subsequent bytes at higher addresses. For "Big Endian" architectures (eg Motorola), the situation is reversed.
不同的计算机体系采用了不同的byte排序。在“Little Endian”体系(例如Intel)中,low-order的字节被放在了内存的最低位,其余字节放在了相对高些的位置。对于“Big Endian”体系(例如Motorola)处理方法则是相反的。
There are two classes in this package of relevance:
* The EndianUtils class contains static methods for swapping the Endian-ness of Java primitives and streams.
EndianUtils类包含了这样的静态方法:对Java的基本类型的和stream的 Endian-ness做交换。
* The SwappedDataInputStream class is an implementation of the DataInput interface. With this, one can read data from files of non-native Endian-ness.
SwappedDataInputStream是DataInput接口的一个实现。通过它可以从一个 non-native Endian-ness 中读数据。
For more information, see http://www.cs.umass.edu/~verts/cs32/endian.html
更多信息,请看 http://www.cs.umass.edu/~verts/cs32/endian.html
Line iterator
The org.apache.commons.io.LineIterator class provides a flexible way for working with a line-based file. An instance can be created directly, or via factory methods on FileUtils or IOUtils. The recommended usage pattern is:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
/// do something with line
} finally {
File filters
The org.apache.commons.io.filefilter package defines an interface (IOFileFilter ) that combines both java.io.FileFilter and java.io.FilenameFilter. Besides that the package offers a series of ready-to-use implementations of the IOFileFilter interface including implementation that allow you to combine other such filters. These filters can be used to list files or in FileDialog, for example.
org.apache.commons.io.filefilter包定义了把java.io.FileFilter和java.io.FilenameFiler组合到一起的接口(IOFileFilter)。另外本包还定义了一系列现成的IOFileFilter实现,也包括你想组合这些filter的实现。这些filter可以用在文件列表 或者 文件对话框中,

See the filefilter package javadoc for more details.
File comparators
The org.apache.commons.io.comparator package provides a number of java.util.Comparator implementations for java.io.File. These comparators can be used to sort lists and arrays of files, for example.
org.apache.commons.io.comparator包提供了一些对java.io.File使用的 java.util.Comparator的实现类,它们可以用来对由文件组成的链表和数组做排序。
See the comparator package javadoc for more details.
The org.apache.commons.io.input and org.apache.commons.io.output packages contain various useful implementations of streams. These include:
org.apache.commons.io.input 和 org.apache.commons.io.output包提供了很多有用的strems接口的实现。包括:
* Null output stream - that silently absorbs all data sent to it
无输出流: 所有发送给它的数据都没反应。
* Tee output stream - that sends output data to two streams instead of one
* Byte array output stream - that is a faster version of the JDK class
字节数组输出流 - JDK中对应类的更快的版本
* Counting streams - that count the number of bytes passed
记数流 - 可以记录通过的字节数。
* Proxy streams - that delegate to the correct method in the proxy
代理流 - 当前代理器中的对应方法的delegate.
* Lockable writer - that provides synchronization of writes using a lock file
可锁定的writer - 使用一个锁定文件实现同步写入。
See the input or output package javadoc for more details.
更多细节请看 input或output包的javadoc.

订阅/RSS Feed