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 输入流复制到 输出流
Writer write = new FileWriter("c:\\kk.dat"); InputStream ins = new FileInputStream(new File("c:\\text.txt")); IOUtils.copy(ins, write); ins.close(); write.close();
copy(InputStream in, OutputStream out )的实现
public static long copyLarge(final InputStream input, final OutputStream output, final byte[] buffer)
throws IOException {
long count = 0;
int n;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
可以发现,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.