Loading

Commin-IO User guide 使用指南

翻译自:http://commons.apache.org/proper/commons-io/description.html#Utility_classes

User guide 使用指南

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

Commons-IO 包含实用程序类、尾数类、行迭代器、文件过滤器、文件比较器和流实现。

For a more detailed descriptions, take a look at the Javadocs.

要了解更详细的描述,请参阅 Javadocs。

Utility classes 实用类

IOUtils

IOUtils contains utility methods dealing with reading, writing and copying. The methods work on InputStream, OutputStream, Reader and Writer.

IOUtils 包含处理读、写和复制的实用方法,这些方法在 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( "https://commons.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( "https://commons.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!

对于这样的实用程序代码,灵活性和速度是最重要的。但是,您也应该了解这种方法的局限性。使用上述技术读取1 GB 文件将导致创建1 GB String 对象的尝试!

FileUtils

The FileUtils class contains utility methods for working with File objects. These include reading, writing, copying and comparing files.

FileUtils 类包含处理 File 对象的实用工具方法,包括读取、写入、复制和比较文件。

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 类包含一些实用工具方法,可以在不使用 File 对象的情况下处理文件名。该类旨在使 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 不支持的功能。目前,唯一的方法是获取驱动器上的空闲空间。注意,这使用了命令行,而不是本机代码。

For example to find the free space on a drive:

例如,查找驱动器上的空闲空间:

 long freeSpace = FileSystemUtils.freeSpace("C:/");

Endian classes

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.

不同的计算机体系结构对字节排序采用不同的约定。在所谓的“ Little Endian”架构(例如 Intel)中,低序字节存储在内存中的最低地址,随后的字节存储在较高地址。对于“大端”架构(例如摩托罗拉) ,情况正好相反。

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 原语和流的 endian 属性的静态方法
  • 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接口的实现。 这样,就可以从非本地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 类为处理基于行的文件提供了一种灵活的方式。可以直接创建实例,也可以通过 FileUtils 或 IOUtils 上的 factory 方法创建实例。建议的使用模式是:

 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包定义了一个接口(IOFileFilter),它包含java.io.FileFilter和java.io.FilenameFilter除此之外,这个包还提供了一系列 IOFileFilter接口的现成实现,包括允许您组合其他这样的过滤器的实现。例如,这些过滤器可用于列出文件或在 FileDialog 中。

See the filefilter package javadoc for more details.

有关详细信息,请参阅 filefilter 包 javadoc。

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.Filejava.util.Comparator 的实现。例如,这些比较器可用于对文件的列表和数组进行排序。

See the comparator package javadoc for more details.

有关详细信息,请参阅 comparator 包 javadoc。

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 and org.apache.commons.io.output 包含各种有用的流实现,包括:

  • Null output stream - that silently absorbs all data sent to it
    空输出流-默默地吸收所有发送给它的数据
  • Tee output stream - that sends output data to two streams instead of one Tee
    输出流——将输出数据发送到两个流而不是一个流
  • 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
    代理流-委托给代理中正确的方法
  • Lockable writer - that provides synchronization of writes using a lock file
    可锁定写入器——使用锁定文件提供写的同步

See the input or output package javadoc for more details.

有关详细信息,请参阅输入或输出包 javadoc。

posted @ 2021-03-25 20:35  克豪  阅读(195)  评论(0)    收藏  举报