使用apache comments io包中的IOUtils中的方法
来源:互联网 发布:ubuntu mate中文指令 编辑:程序博客网 时间:2024/06/06 01:06
- 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("--------------------------------------------------------------");
- }
- }
0 0
- 学习使用apache comments io包中的IOUtils中的方法
- 使用apache comments io包中的IOUtils中的方法
- 学习使用apache comments io包中的IOUtils中的方法
- 学习使用apache comments io包中的IOUtils中的方法
- Apache commons-io 包中的CharSet编码用IOUtils 读文件,在Hadoop下问题
- 工具类commons-io中的IOUtils的使用
- 使用Apache.commons.io文件工具类FileUtils,IOUtils使用
- apache_commons_io中的IOUtils学习
- 模仿apache jar包中的,copyProperty方法
- org.apache.commons.io.IOUtils IOUtils.copy与IOUtils.closeQuietly的用法
- 使用org.apache.commons.io.FileUtils,IOUtils;工具类操作文件
- 使用org.apache.commons.io.FileUtils,IOUtils;工具类操作文件
- 使用org.apache.commons.io.FileUtils,IOUtils;工具类操作文件
- Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils
- java.io包下的read()方法中的怪现象
- org.apache.commons.io.IOUtils.copyLarge源码解析
- IO包中的其他对象
- IO包中的其他对象
- 关于网页动态数据获取的知识学习(1)
- 读取两文件,不同的内容存入另一个文件中
- Swift源码分析----swift-proxy与swift-container
- Linux的inode的理解
- 如何获取oracle dbid
- 使用apache comments io包中的IOUtils中的方法
- java,jsp获取上一个访问页面URL
- 大神帮我看看错了吗??
- 关于概率性事件的产品性能和客户体验讨论
- 关于网页动态数据获取的知识学习(2)
- 银行业务调度系统
- UNIX下C语言动态链接库SO
- 认识他那么容易的事那是人家那天生日
- 复制的没那个好像没那想法给你下功夫