Back

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

发布时间: 2008-05-06 07:36:00

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
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:
例如,从一个URL中读取字节,然后打印出它们。一般代码是这样写的:
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 {
in.close();
}
With the IOUtils class, that could be done with:
使用了IOUtils类之后,可以这样写:
InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
try {
System.out.println( IOUtils.toString( in ) );
} finally {
IOUtils.closeQuietly(in);
}
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.
在某些应用领域,这样的IO操作非常常见,而使用该类可以节省大量时间。请放心的使用这些经过良好测试的代码。
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!
对于工具类中的代码来讲,灵活性和速度是最重要的。然而我们也要知道它的局限性。使用上面的代码来读一个1GB的文件,程序将试图产生一个1GB的String对象!(译注:我晕)

FileUtils
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");
FilenameUtils
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).
FilenameUtils类包含了在不使用文件对象的情况下对文件名进行操作的方法。该类的目标是使Unix和Windows之间的代码转换(例如从开发环境移植到部署环境)更容易成功。
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"
FileSystemUtils
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:
org.apache.commons.io.LineIterator提供了对于line-based的文件的灵活的操作方式。可以直接建立一个该类的实例,或者使用FileUtils和IOUtils提供的工厂方法。建议的这样用:
LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
/// do something with line
}
} finally {
LineIterator.closeQuietly(iterator);
}
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.
更多信息请看filefilter包。
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.
更多细节请看comparator包。
Streams
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
T字型输出流:一个输入会产生两个输出
* 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.

Back