apache FileUtils 和 IOUtils 工具类 改写普通文件读写方式 提高效率

来源:互联网 发布:ti 知乎 编辑:程序博客网 时间:2024/06/05 21:05

不重复开发的轮子,既然人家已经写好了通用成熟的工具,与其自己吭哧吭哧写通用类,还不如拿来用即可。但是用归用,不了解还是不行滴,咻咻,

apache下 开源了许多实用的工具类,一般都是在commons包下。

开源的在线文档:http://commons.apache.org/

commons.jar 包下载 :http://commons.apache.org/io/download_io.cgi

这里讲到了:  IOUtils 和 FileUtils 。 中文文档奇缺,有知道的童鞋可以吱一声。

1.IOUtils 简要说明

该类为input/output操作提供了通用静态方法。

包括功能:

•closeQuietly——这些关闭一个流的方法将忽略nulls和例外的情况。

•toXxx /read-这些方法从一个流读取数据。

• write这些方法将数据写到一个流里。

copy——这些方法用于从一个流的所有的数据复制到另一个流。

•contentEquals——这些方法用于比较两个流的内容。

 

byte-to-char方法集和char-to-byte方法集都包括一个转换步骤。给出的两种方法集合在每种情况下,一种是使用平台默认的编码方式,另一个允许你指定一个编码。

鼓励你总是指定一个编码因为依靠平台违约会带来意想不到的结果,例如从开发阶段移动到产品部署。

 

这个类里面所有的方法在读一个流的时候内部是被缓冲的。这意味着我们外部使用的时候就不需要BufferedInputStream或BufferedReader。在测试中默认缓存4 K已被证明是有效。

无论在何处,在这个类里的方法在这堂课里不要冲洗或关闭流。这是为了避免犯不可移植的假设那小溪的来源和进一步使用。因此调用者还需要负责使用后手动关闭流。

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------

 使用IOUtils来重写原来的读取写入流的操作,代码简洁,没了烦人的try  以前连基本的操作都要在两个流之间倒来倒去 还要考虑怎么转换,现在一切都是浮云了。

--------------------------------------------------------------------------------------------------------------------------------------------------------------

这边是API的简单介绍:

1.closeQuietly 包括对各种类型输入输出流的关闭。 无条件的关闭流。

[java] view plain copy
  1. byte[] data = "Hello, World".getBytes();   
  2.   
  3. OutputStream out = null;  
  4.   
  5.  try {   
  6.   
  7.     out = new FileOutputStream("foo.txt");   
  8.   
  9.     out.write(data);   
  10.   
  11.     //out.close();//close errors are handled  我们通常的用法,是不太好的。  
  12.   
  13.     }   
  14.   
  15. catch (IOException e) {   
  16.   
  17. // error handling   
  18.   
  19. finally  
  20.   
  21.  {  
  22.   
  23.   IOUtils.closeQuietly(out); 以后就直接在finally里关闭流。也不需要判断条件。  
  24.   
  25.  }  


2. toByteArray 从各类输入流中读取数据并转换为字节数组。

toByteArray(参数) 里的参数包括:Reader,InputStream,String,这里String 已经过时,等同于String.getBytes();

toByteArray

public static byte[] toByteArray(InputStream input,                                 long size)                          throws IOException

以字节数组的形式来获取inputStream流里的内容,如果在流的大小已知的情况下,可以用此方法来代替默认的toByteArray(InputStream input)方法。

注:该方法会检查size是否可以安全地转换为int,在其正式读到字节数组前。

 

①读操作

IOUtils类提供的读操作方法有两大类:第一类是readLines方法。第二类是toXxx方法。

 ※ readLines方法

List readLines(InputStream input)
List readLines(InputStream input, String encoding)
readLines(Reader input)


我们知道在字节流中是没有“行”的概念的,但是为什么这里的readLines方法可以接收InputStream呢?看看源代码就知道了

public static List readLines(InputStream input, String encoding) throws IOException {
        
if (encoding == null{
            
return readLines(input);
        }
 else {
            InputStreamReader reader = new InputStreamReader(input, encoding);

            
return readLines(reader);
        }

    }


public static List readLines(Reader input) throws IOException {
        BufferedReader reader 
= new BufferedReader(input);
        List list 
= new ArrayList();
        String line 
= reader.readLine();
        
while (line != null{
            list.add(line);
            line 
= reader.readLine();
        }

        
return list;
    }


原来在底层,IOUtils使用了InputStreamReader对input stream进行了包装,到了readLines(Reader)方法内,又再加了一个缓冲。如果我们是直接调用readLines(Reader)方法,为了确保编码正确,需要手工创建一个InputStreamReader并指明encoding,否则将采用默认的encoding。

 ※ toXxx方法

IOUtils支持把inputStream中的数据转换成byte[],char[],String对象。而且input stream可以是字节流,字符流。同时可以指定encoding。这些方法实质上是“输出”的过程:即从输入流中读入数据,然后转换为byte[],char[],String,输出到内存中。看看下面的一个源代码:

    public static char[] toCharArray(InputStream is, String encoding)
            
throws IOException {
        CharArrayWriter output 
= new CharArrayWriter();
        copy(is, output, encoding);
        
return output.toCharArray();
    }


    
public static void copy(InputStream input, Writer output, String encoding)
            
throws IOException {
        
if (encoding == null{
            copy(input, output);
        }
 else {
          InputStreamReader in = new InputStreamReader(input, encoding);

            copy(in, output);
        }

    }


    
public static int copy(Reader input, Writer output) throws IOException {
        
long count = copyLarge(input, output);
        
if (count > Integer.MAX_VALUE) {
            
return -1;
        }

        
return (int) count;
    }


    
public static long copyLarge(Reader input, Writer output) throws IOException {
        
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        
long count = 0;
        
int n = 0;
        
while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);

            count 
+= n;
        }

        
return count;
    }



我们可以看到这个过程是没有进行flush的操作的,也就是说使用者必须负责在调用结束后进行缓存清空和输入、输入流关闭。对于input stream是文件的情况,在FileUtils的文件读方法的最后都会调用IOUtils.closeQuietly(in);方法来确保输入流正确关闭。

②写操作

和读操作一样,IOUtils一样提供了大量的写方法,这些方法可以将byte[],char[],StringBuffer,String,Collection中的数据以字节流,字符流的形式写入到目的源。

 ※ writeLines方法

public static void writeLines(Collection lines, String lineEnding,
            OutputStream output, String encoding) 
throws IOException {
        
if (encoding == null{
            writeLines(lines, lineEnding, output);
        }
 else {
            
if (lines == null{
                
return;
            }

            
if (lineEnding == null{
                lineEnding 
= LINE_SEPARATOR;
            }

            
for (Iterator it = lines.iterator(); it.hasNext(); ) {
                Object line 
= it.next();
                
if (line != null{
                    output.write(line.toString().getBytes(encoding));
                }

                output.write(lineEnding.getBytes(encoding));
            }

        }

    }


public static void writeLines(Collection lines, String lineEnding,
            Writer writer) 
throws IOException {
        
if (lines == null{
            
return;
        }

        
if (lineEnding == null{
            lineEnding 
= LINE_SEPARATOR;
        }

        
for (Iterator it = lines.iterator(); it.hasNext(); ) {
            Object line 
= it.next();
            
if (line != null{
                writer.write(line.toString());
            }

            writer.write(lineEnding);
        }

    }


如果我们查看FileUtils,会发现它对所有的文件读写(包括writeLines,writeStringToFile),都是调用字节流+encoding的方式来进行的。因为所有基于字符流的方式最终都需要转换为基于字节流的方式。

③流拷贝

我们在从文件等数据源读入数据时,习惯性地以字节读入,到了内存又转换成String对象,最后修改性地以字符写回文件。IOUtils提供了一系列方便的方法来进行这中间的转换。

copy(InputStream input, Writer output, String encoding),这个方法使用指定的encoding,从字节流中读入字节,然后按照encoding解码,通过字符流写回目的源。

copy(Reader input, OutputStream output, String encoding),这个方法从字符流中读取字符,使用指定的encoding编码,通过字节流写回目的源,然后立即清空缓冲。

上面这两个方法底层都调用了一个名为copyLarge的方法,他们分别在通过一个byte[]或者char[]数组对要写回的内容进行缓冲。一次性地从源端读入4K数据然后通过输出流写回。

    public static long copyLarge(InputStream input, OutputStream output)
            
throws IOException {
        
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        
long count = 0;
        
int n = 0;
        
while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 
0, n);
            count 
+= n;
        }

        
return count;
    }


    
public static long copyLarge(Reader input, Writer output) throws IOException {
        
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        
long count = 0;
        
int n = 0;
        
while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 
0, n);
            count 
+= n;
        }

        
return count;
    }

阅读全文
0 0
原创粉丝点击