学习使用apache comments io包中的IOUtils中的方法

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中的方法
 * @author 王震
 *
 */
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上的开源项目,很强悍的。哈
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值