IO与文件读写---使用Apache commons io包提高读写效率

本文介绍了Apache Commons IO库,包括其主要功能和实用工具类,如AutoCloseInputStream和ByteArrayOutputStream。AutoCloseInputStream能确保输入流在读取结束或显式关闭时及时关闭,释放资源。而ByteArrayOutputStream提供高效的字节数组输出流,初始缓冲区更大,且动态扩展以减少内存管理和调用开销。
摘要由CSDN通过智能技术生成

【一】Apache commons IO简介

首先贴一段Apache commons IO官网上的介绍,来对这个著名的开源包有一个基本的了解:

Commons IO is a library of utilities to assist with developing IO functionality. There are four main areas included: 

 ●
Utility classes - with static methods to perform common tasks 
 ●Filters - various implementations of file filters 
 ●Comparators - various implementations of java.util.Comparator for files
 ●Streams - useful stream, reader and writer implementations

Packages
org.apache.commons.io This package defines utility classes for working with streams, readers, writers and files.
org.apache.commons.io.comparator This package provides various Comparator implementations for Files.
org.apache.commons.io.filefilter This package defines an interface (IOFileFilter) that combines both FileFilter and FilenameFilter.
org.apache.commons.io.input This package provides implementations of input classes, such as InputStream and Reader.
org.apache.commons.io.output This


【二】org.apache.comons.io.input包介绍

这个包针对SUN JDK IO包进行了扩展,实现了一些功能简单的IO类,主要包括了对字节/字符输入流接口的实现


这个包针对java.io.InputStream和Reader进行了扩展,其中比较实用的有以下几个:

●AutoCloseInputStream

Proxy stream that closes and discards the underlying stream as soon as the end of input has been reached or when the stream is explicitly closed. Not even a reference to the underlying stream is kept after it has been closed, so any allocated in-memory buffers can be freed even if the client application still keeps a reference to the proxy stream

This class is typically used to release any resources related to an open stream as soon as possible even if the client application (by not explicitly closing the stream when no longer needed) or the underlying stream (by not releasing resources once the last byte has been read) do not do that. 

这个输入流是一个底层输入流的代理,它能够在数据源的内容被完全读取到输入流后,后者当用户调用close()方法时,立即关闭底层的输入流。释放底层的资源(例如文件的句柄)。这个类的好处就是避免我们在代码中忘记关闭底层的输入流而造成文件处于一直打开的状态。

我们知道对于某些文件,只允许由一个进程打开。如果我们使用后忘记关闭那么该文件将处于一直“打开”的状态,其它进程无法读写。例如下面的例子:

 new BufferedInputStream(new FileInputStream(FILE))

里面的FileInputStream(FILE)在打开后不能被显式关闭,这将导致可能出现的问题。如果我们使用了AutoCloseInputStream,那么当数据读取完毕后,底层的输入流会被自动关闭,迅速地释放资源。

 new BufferedInputStream(new AutoClosedInputStream(new FileInputStream));

那么这个类是如何做到自动关闭的呢?来看看这个非常简单的类的代码吧


package  org.apache.commons.io.input;

import  java.io.IOException;
import  java.io.InputStream;


public   class  AutoCloseInputStream  extends  ProxyInputStream  {

   
public AutoCloseInputStream(InputStream in) {
        
super(in);
    }


    
public void close() throws IOException {
        in.close();
        in 
= new ClosedInputStream();
    }


  
public int read() throws IOException {
        
int n = in.read();
        
if (n == -1{
            close();
        }

        
return n;
    }


    
public int read(byte[] b) throws IOException {
        
int n = in.read(b);
        
if (n == -1{
            close();
        }

        
return n;
    }


 
    
public int read(byte[] b, int off, int len) throws IOException {
        
int n = in.read(b, off, len);
        
if (n == -1{
            close();
        }

        
return n;
    }


    
protected void finalize() throws Throwable {
        close();
        
super.finalize();
    }


}


public   class  ClosedInputStream  extends  InputStream  {
    
    
/**
     * A singleton.
     
*/

    
public static final ClosedInputStream CLOSED_INPUT_STREAM = new ClosedInputStream();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值