IOUtils工具类简介及应用
生活随笔
收集整理的這篇文章主要介紹了
IOUtils工具类简介及应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
以前寫文件的復制很麻煩,需要各種輸入流,然后讀取line,輸出到輸出流...其實apache.commons.io里面提供了輸入流輸出流的常用工具方法,非常方便。下面就結合源碼,看看IOUTils都有什么用處吧!
?
copy
源碼介紹:這個方法可以拷貝流,算是這個工具類中使用最多的方法了。支持多種數據間的拷貝
copy(inputstream,outputstream) copy(inputstream,writer) copy(inputstream,writer,encoding) copy(reader,outputstream) copy(reader,writer) copy(reader,writer,encoding)?copy內部使用的其實還是copyLarge方法。因為copy能拷貝Integer.MAX_VALUE的字節數據,即2^31-1。
copyLarge
源碼介紹:這個方法適合拷貝較大的數據流,比如2G以上。
copyLarge(reader,writer) 默認會用1024*4的buffer來讀取 copyLarge(reader,writer,buffer)?第二個內部的細節可以參考:
public static long copyLarge(Reader input, Writer output, char [] buffer) throws IOException {long count = 0;int n = 0;while (EOF != (n = input.read(buffer))) {output.write(buffer, 0, n);count += n;}return count;}這個方法會用一個固定大小的Buffer,持續不斷的讀取數據,然后寫入到輸出流中。?
toBufferedInputStream?
源碼介紹:把流的全部內容放在另一個流中?
public static InputStream toBufferedInputStream(InputStream input) throws IOException {return ByteArrayOutputStream.toBufferedInputStream(input);}public static InputStream toBufferedInputStream(InputStream input, int size) throws IOException {return ByteArrayOutputStream.toBufferedInputStream(input, size);}實際應用
InputStream in = classPathResource.getInputStream();InputStream inputStream = IOUtils.toBufferedInputStream(in);?
?
參考來源于:
http://www.cnblogs.com/xing901022/p/5978989.html
總結
以上是生活随笔為你收集整理的IOUtils工具类简介及应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 应用在启动后进行全局的的
- 下一篇: MySQL Server Archite