日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

apache comments io包IOUtils方法简单介绍

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 apache comments io包IOUtils方法简单介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?IOUtils封裝了Java中io的常見操作,使用十分方便。需要下載?commons-io-1.4.jar?包

package com.tiewoba.apache.comments;import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Iterator; import java.util.List;import org.apache.commons.io.IOUtils; import org.apache.commons.io.LineIterator; /*** 學習使用apache comments io包中的IOUtils中的方法**/ public class TestComment {public static void main(String[] args) throws MalformedURLException,IOException {InputStream in = new URL("http://www.apache.org").openStream();InputStream in2 = new URL("http://www.apache.org").openStream();/*** IOUtils.contentEquals(InputStream input1, InputStream input2)* 英文:* Compare the contents of two Streams to determine if they are equal or not.* 翻譯:* 比較兩個輸入流的內容是否相等*/try {//IOUtils.toString將緩沖區的內容以utf-8的編碼方式以字符串的形式輸出//System.out.println(IOUtils.toString(in,"UTF-8"));} finally {IOUtils.closeQuietly(in);}System.out.println(IOUtils.contentEquals(in, in2));System.out.println("-----------------------------------------------------------------------");/*** IOUtils.copy(InputStream input, OutputStream output)* 英文:* Copy bytes from an InputStream to an OutputStream.* 翻譯:* 將字節從 InputStream復制到OutputStream中*/ByteArrayOutputStream out = new ByteArrayOutputStream();IOUtils.copy(in2, out);System.out.println(out.size());//System.out.println(out.toString("UTF-8"));//IOUtils.closeQuietly(in);//IOUtils.closeQuietly(out);System.out.println("-----------------------------------");/**IOUtils.copyLarge(InputStream input, OutputStream output);* 英文* Copy bytes from a large (over 2GB) InputStream to an OutputStream.* 翻譯:* 將字節超過2G的字節輸入流復制到輸出流中*/IOUtils.copyLarge(in2, out);//System.out.println(out.toString("UTF-8"));System.out.println("------------------------------------------------------------");/*** IOUtils.lineIterator(InputStream input, Charset encoding)?* 英文* Return an Iterator for the lines in an InputStream, using the character encoding specified (or default encoding if null).* 翻譯* 返回一個裝有輸入字節行數的Iterator對象,使用特定的字符編碼(如果沒用聲明的話則用默認編碼)*/InputStream in3 = new URL("http://www.apache.org").openStream();try {LineIterator it = IOUtils.lineIterator(in3, "UTF-8");System.out.println(it.hasNext());if (it.hasNext()) {String line = it.nextLine();//System.out.println(line);}} finally {IOUtils.closeQuietly(in);}System.out.println("------------------------------------------------------------------");/*** IOUtils.read(InputStream input, byte[] buffer)* 英文:* Read bytes from an input stream.* 中文:從輸入流中讀取字節(通常返回輸入流的字節數組的長度)*/InputStream in4 = new URL("http://www.apache.org").openStream();byte[] buffer = new byte[100000];System.out.println(IOUtils.read(in4, buffer));System.out.println("------------------------------------------------");/*** IOUtils.readLines(InputStream input, Charset encoding)?* 英文:* Get the contents of an InputStream as a list of Strings, one entry per line, using the specified character encoding.* 翻譯:* 獲得輸入流的內容放回一個List<String>類型的容器,每一行為這個容器的一個入口,使用特定的字符集(如果為空就使用默認的字符集) ?*/InputStream in5 = new URL("http://www.apache.org").openStream();List<String> list = IOUtils.readLines(in5, "UTF-8");Iterator<String> iter = list.iterator();while(iter.hasNext()){String s = ?iter.next();//System.out.println(s);}System.out.println("--------------------------------------------------------------");}}

?

總結

以上是生活随笔為你收集整理的apache comments io包IOUtils方法简单介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。