`

Apache common io overview

    博客分类:
  • J2SE
阅读更多

Apache 里面包含了太多有用的项目,值得我们仔细研究。恰当的应用这些工具,能有效提高我们的开发效率,下面我们先来看一下io吧,觉得平常用apache的io 以及file 工具类已经很多了,但是没想到今天看了一下,还是有一些没有用过,汗一个。

 

Utility classes

IOUtils

IOUtils contains utility methods dealing with reading, writing and copying. The methods work on InputStream, OutputStream, Reader and 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 {
   in.close();
 }

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 {
   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.

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!

FileUtils

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

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).

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.

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.

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.
  • The SwappedDataInputStream class is an implementation of the DataInput interface. With this, one can read data from files of non-native Endian-ness.

For more information, see 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 onFileUtils 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 {
   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.

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.

See the comparator package javadoc for more details.

Streams

The org.apache.commons.io.input and org.apache.commons.io.output packages contain various useful implementations of streams. These include:

  • 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
  • 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

分享到:
评论

相关推荐

    java io tutorial overview

    ### Java IO 教程概述:理解输入与输出 在深入探讨Java IO(输入输出)教程之前,我们首先需要澄清一个常见的概念混淆:“输入”与“输出”。这两个术语在不同的上下文中可能让人感到迷惑,尤其是在软件工程领域。...

    struts2 的类库 commons-io-1.3.2 文档

    在Struts2框架中,Apache Commons IO是一个关键的类库,它提供了大量的实用工具类来处理输入/输出操作。这个文档包"commons-io-1.3.2"是针对该版本的API详细参考,对开发者来说是非常有价值的资源。 Apache Commons...

    第20课时.PN-IO-OVERVIEW03.mp4

    西门子S7-300中高级应用技术

    [Apache Karaf] Apache Karaf 学习教程 (英文版)

    Overview Understand Apache Karaf's commands and control capabilities Gain familiarity with its provisioning features Explore various application deployments targets experientially ☆ 出版信息:☆ ...

    OPC Overview1.0.pdf

    Handling or Historical DataAccess) and a specification for interfaces that are common for all OPC Servers are available as separate documents. Chapter 1 gives some background information. It ...

    Linux-IO性能优化基础工具和实践

    基础篇-Linux IO stack overview 基础篇-read syscall IO stack 工具篇-iostat数据可靠吗 工具篇-blktrace原理和应用 工具篇-debugfs应用 Cache server机械盘IO性能瓶颈分析 实践篇-IO性能优化之文件压缩 实践篇-IO...

    Learning Apache Karaf

    Overview Understand Apache Karaf’s commands and control capabilities Gain familiarity with its provisioning features Explore various application deployments targets experientially In Detail Apache ...

    An Overview of Multi-Task Learning in Deep Neural Networks.pdf

    It introduces the two most common methods for MTL in Deep Learning, gives an overview of the literature, and discusses recent advances. In particular, it seeks to help ML practitioners apply MTL ...

    overview_20200306_V4_overview_氛围灯_vbaexcel_

    标题中的"overview_20200306_V4_overview_氛围灯_vbaexcel_"暗示了这是一个关于2020年3月6日版本的车内氛围灯系统概述,利用VBA(Visual Basic for Applications)和Excel进行自动化管理的项目。这个系统可能是为了...

    Apache Spark 2.4 and beyond

    Xiao Li and Wenchen Fan offer an overview of the major features and enhancements in Apache Spark 2.4. Along the way, you’ll learn about the design and implementation of V2 of theData Source API and ...

    SAP 中文版OverviewSAP 中文版Overview

    "SAP 中文版Overview" 在这个 SAP 中文版Overview 中,我们将详细介绍 SAP 的概念、结构、模块、导航和业务流程等知识点。 MRP、MRPII 和 ERP 的概念 MRP(Material Requirements Planning,物料需求规划)是根据...

    eclipse overview插件

    eclipse overview插件 跟sublime 的预览插件一样效果 由于无法在线安装,可以使用link方式安装 步骤如下 1、下载好解压文件,解压到指定目录如: D:\Program Files (x86)\eclipse\overview目录 2、找到eclipse安装...

    SAP SD overview

    SAP SD overview SAP SD overview SAP SD overview SAP SD overview

    [Apache Flume] Apache Flume 分布式日志采集应用 (Hadoop 实现) (英文版)

    Overview Integrate Flume with your data sources Transcode your data en-route in Flume Route and separate your data using regular expression matching Configure failover paths and load-balancing to ...

    openEHR Overview

    openEHR Overview openEHR的概要介绍

    Apache Hadoop 3 Quick Start Guide

    The book begins with an overview of big data and Apache Hadoop. Then, you will set up a pseudo Hadoop development environment and a multi-node enterprise Hadoop cluster. You will see how the parallel ...

    hr+overview.ppt

    SAP: hr+overview.ppt

Global site tag (gtag.js) - Google Analytics