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

 

通过IOUtils 我们能够提升IO读写效率。下面是一些简单分析。

 

IOUtils 输入流复制到 输出流

1 Writer write = new FileWriter("c:\kk.dat");  
2 
3 InputStream ins = new FileInputStream(new File("c:\text.txt"));  
4  
5 IOUtils.copy(ins, write);  
6 
7 ins.close(); write.close();

copy(InputStream in, OutputStream out )的实现

 1     public static long copyLarge(final InputStream input, final OutputStream output, final byte[] buffer)
 2             throws IOException {
 3         long count = 0;
 4         int n;
 5         while (EOF != (n = input.read(buffer))) {
 6             output.write(buffer, 0, n);
 7             count += n;
 8         }
 9         return count;
10     }

可以发现,copy流的方法直接写入而不仅仅是定义OutputStream,这点开发中一定要注意。 ^_^

IOUtils的closeQuietly()方法:

  Unconditionally close an InputStream.

  Equivalent to InputStream.close(), except any exceptions will be ignored. This is typically used in finally blocks.

原文地址:https://www.cnblogs.com/mywy/p/5026541.html