转自:http://blog.csdn.net/zhaoyanjun6/article/details/55051917

福利
另外我已经把Commons IO 2.5的源码发布到Jcenter,大家就不用下载jar包了,可以直接引用。

Maven引用
<dependency>
<groupId>org.apache.commons.io</groupId>
<artifactId>commonsIO</artifactId>
<version>2.5.0</version>
<type>pom</type>
</dependency>

Gradle引用
compile 'org.apache.commons.io:commonsIO:2.5.0'
1
IOUtils流操作的相关方法
copy:这个方法可以拷贝流,算是这个工具类中使用最多的方法了。支持多种数据间的拷贝。copy内部使用的其实还是copyLarge方法。因为copy能拷贝Integer.MAX_VALUE的字节数据,即2^31-1。
copy(inputstream,outputstream)
copy(inputstream,writer)
copy(inputstream,writer,encoding)
copy(reader,outputstream)
copy(reader,writer)
copy(reader,writer,encoding)

copyLarge:这个方法适合拷贝较大的数据流,比如2G以上。
copyLarge(reader,writer) 默认会用1024*4的buffer来读取
copyLarge(reader,writer,buffer)

获取输入流
//通过文本获取输入流 , 可以指定编码格式
InputStream toInputStream(final String input, final Charset encoding)

InputStream toInputStream(final String input, final String encoding)

//获取一个缓冲输入流,默认缓冲大小 1KB
InputStream toBufferedInputStream(final InputStream input)

//获取一个指定缓冲流的大小的输入流
InputStream toBufferedInputStream(final InputStream input, int size)

//把流的全部内容放在另一个流中
BufferedReader toBufferedReader(final Reader reader)

//把流的全部内容放在另一个流中
BufferedReader toBufferedReader(final Reader reader, int size)

获取输入流里面的内容
// 输入流 --》 字符串
String toString(final InputStream input, final Charset encoding)

// 输入流 --》 字符串
String toString(final InputStream input, final String encoding)

// 字符输入流 --》 字符串
String toString(final Reader input)

// 字符数组 --》 字符串
String toString(final byte[] input, final String encoding)

//输入流 --》 字符数组
byte[] toByteArray(final InputStream input)

//输入流 --》 字符数组
byte[] toByteArray(final Reader input, final Charset encoding)

//输入流 --》 字符数组
byte[] toByteArray(final Reader input, final String encoding)

//URL --》 字符数组
byte[] toByteArray(final URI uri)

// URL --》 字符串
String toString(final URL url, final Charset encoding)

// URL --》 字符串
String toString(final URL url, final String encoding)

// URLConnection --》 字符串
byte[] toByteArray(final URLConnection urlConn)

字符串读写
List<String> readLines(InputStream input)

List<String> readLines(InputStream input, final Charset encoding)

List<String> readLines(InputStream input, final String encoding)

List<String> readLines(Reader input)

void writeLines(Collection<?> lines, String lineEnding, OutputStream output)

void writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding)

void writeLines(Collection<?> lines, String lineEnding, OutputStream output, final encoding)

void writeLines(Collection<?> lines, String lineEnding,Writer writer)

小例子:

public void readLinesTest(){
try{
InputStream is = new FileInputStream("D://test1.txt");
List<String> lines = IOUtils.readLines(is);
for(String line : lines){
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

结果:

hello
world

write:这个方法可以把数据写入到输出流中
write(byte[] data, OutputStream output)
write(byte[] data, Writer output)
write(byte[] data, Writer output, Charset encoding)
write(byte[] data, Writer output, String encoding)

write(char[] data, OutputStream output)
write(char[] data, OutputStream output, Charset encoding)
write(char[] data, OutputStream output, String encoding)
write(char[] data, Writer output)

write(CharSequence data, OutputStream output)
write(CharSequence data, OutputStream output, Charset encoding)
write(CharSequence data, OutputStream output, String encoding)
write(CharSequence data, Writer output)

write(StringBuffer data, OutputStream output)
write(StringBuffer data, OutputStream output, String encoding)
write(StringBuffer data, Writer output)

write(String data, OutputStream output)
write(String data, OutputStream output, Charset encoding)
write(String data, OutputStream output, String encoding)
write(String data, Writer output)

read:从一个流中读取内容
read(inputstream,byte[])
read(inputstream,byte[],offset,length)
//offset是buffer的偏移值,length是读取的长度

read(reader,char[])
read(reader,char[],offset,length)

下例子:

public void readTest(){
try{
byte[] bytes = new byte[4];
InputStream is = IOUtils.toInputStream("hello world");
IOUtils.read(is, bytes);
System.out.println(new String(bytes));

bytes = new byte[10];
is = IOUtils.toInputStream("hello world");
IOUtils.read(is, bytes, 2, 4);
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}

readFully:这个方法会读取指定长度的流,如果读取的长度不够,就会抛出异常
readFully(inputstream,byte[])
readFully(inputstream,byte[],offset,length)
readFully(reader,charp[])
readFully(reader,char[],offset,length)

小例子:

public void readFullyTest(){
byte[] bytes = new byte[4];
InputStream is = IOUtils.toInputStream("hello world");
try {
IOUtils.readFully(is,bytes);
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
}

输出:hell
报出异常:

java.io.EOFException: Length to read: 20 actual: 11
at org.apache.commons.io.IOUtils.readFully(IOUtils.java:2539)
at org.apache.commons.io.IOUtils.readFully(IOUtils.java:2558)
at test.java.IOUtilsTest.readFullyTest(IOUtilsTest.java:22)
...

contentEquals:比较两个流是否相等
contentEquals(InputStream input1, InputStream input2)
contentEquals(Reader input1, Reader input2)

contentEqualsIgnoreEOL:比较两个流,忽略换行符
contentEqualsIgnoreEOL(Reader input1, Reader input2)

skip:这个方法用于跳过指定长度的流
long skip(inputstream,skip_length)
long skip(ReadableByteChannel,skip_length)
long skip(reader,skip_length)

skipFully:这个方法类似skip,只是如果忽略的长度大于现有的长度,就会抛出异常。
skipFully(inputstream,toSkip)
skipFully(readableByteChannel,toSkip)
skipFully(inputstream,toSkip)

小例子:

public void skipFullyTest(){
InputStream is = IOUtils.toInputStream("hello world");
try {
IOUtils.skipFully(is,30);
System.out.println(IOUtils.toString(is,"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}

ineIterator:读取流,返回迭代器
LineIterator lineIterator(InputStream input, Charset encoding)
LineIterator lineIterator(InputStream input, String encoding)
LineIterator lineIterator(Reader reader)

close:关闭流

//关闭 URLConnection
void close(final URLConnection conn)

//closeQuietly 忽略nulls和异常,关闭某个流
void closeQuietly(final Reader input)

void closeQuietly(final Writer output)

void closeQuietly(final InputStream input)

void closeQuietly(final OutputStream output)

void closeQuietly(final Socket sock)

void closeQuietly(final ServerSocket sock)

小例子1

package com.app;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;

public class A8 {

public static void main(String[] args) {

String meString = "哈哈哈哈,下班了,hello" ;

try {

InputStream inputStream = IOUtils.toInputStream( meString, "utf-8" ) ;
String mes = IOUtils.toString( inputStream , "utf-8" ) ;
System.out.println( mes );

} catch (IOException e) {
e.printStackTrace();
}
}
}

效果:

哈哈哈哈,下班了,hello
1
小例子2 : 模拟了http 请求
package com.app;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.IOUtils;

public class A8 {

public static void main(String[] args) {

String meString = "http://www.baidu.com" ;

try {
//模拟了http 请求
String mes = IOUtils.toString( new URL( meString ) , "utf-8") ;
System.out.println( mes );

} catch (IOException e) {
e.printStackTrace();
}
}
}

Commons IO 2.5-IOUtils的更多相关文章

  1. apache.commons.io.IOUtils: 一个很方便的IO工具库(比如InputStream转String)

    转换InputStream到String, 比如 //引入apache的io包 import org.apache.commons.io.IOUtils; ... ...String str = IO ...

  2. Commons IO - IOUtils

    IOUtils is a general IO stream manipulation utilities. This class provides static utility methods fo ...

  3. Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils

    关键词:IntelliJ IDEA.Tomcat.commons-io-2.5.jar.java.lang.ClassNotFoundException: org.apache.commons.io. ...

  4. Commons IO方便读写文件的工具类

    Commons IO是apache的一个开源的工具包,封装了IO操作的相关类,使用Commons IO可以很方便的读写文件,url源代码等. 普通地读取一个网页的源代码的代码可能如下 InputStr ...

  5. Apache Commons IO 2.3 几点用法

    //直接将IO流转成字符串 InputStream in = new URL( "http://jakarta.apache.org" ).openStream(); try { ...

  6. Java IO流学习总结八:Commons IO 2.5-IOUtils

    Java IO流学习总结八:Commons IO 2.5-IOUtils 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/550519 ...

  7. [转]Commons IO 官方文档

    原文地址:http://ifeve.com/commons-io/ 本文翻译自 Commons IO 官方文档 译者:MagicWolf Common IO 是一个工具库,用来帮助开发IO功能 它包括 ...

  8. apache.commons.io.FileUtils的常用操作

    至于相关jar包可以到官网获取 http://commons.apache.org/downloads/index.html package com.wz.apache.fileUtils; impo ...

  9. Commons IO

    Common IO 是一个工具库,用来帮助开发IO功能 它包括6个主要部分 Utility classes – 包括一些静态方法来执行常用任务 Input – InputStream 和 Reader ...

随机推荐

  1. keil mdk uvision使用技巧

    语法检测&代码提示 中文友好: tab 可以选中一大块代码,一起缩进 快速注释 先选中你要注释的代码区,然后右键,选择Advanced,Comment Selection 就可以了 查找替换 ...

  2. Jmeter关联-获取token值

    1. token就是令牌,比如你授权(登录)一个程序时,他就是个依据,判断你是否已经授权该软件:也叫关联 2. cookie就是写在客户端的一个txt文件,里面包括你登录信息之类的,这样你下次在登录某 ...

  3. xtu summer individual 3 F - Opening Portals

    Opening Portals Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  4. set/multiset用法详解

    集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...

  5. ZOJ3953 ZJU2017校赛(贪心)

    题意:给出n个区间,求至少删掉多少个区间使得不存在区间a, b, c 两两相交    (定义两个区间相交是,区间[l1, r1]和区间[l2, r2]相交,当且仅当存在一个数x,l1<=x< ...

  6. IE插件

    在OA上要直接查看word等公告文件,就必须安装office控件.要安装office控件,需要在IE浏览器中做相应的设置.如何设置呢,下面由小编具体介绍下. 工具/原料   OA IE浏览器 方法/步 ...

  7. E-SATA接口

    1.VGA接口 VGA接口就是显卡上输出模拟信号的接口,也叫D-Sub接口,其实就是显示转移的接口,比如连接投影仪.连接电视等等.从外观上讲,VGA接口是一种D型接口,上面共有15针空,分成三排,每排 ...

  8. spring 拦截器整体配置

    1.spring boot拦截器默认有 HandlerInterceptorAdapter AbstractHandlerMapping UserRoleAuthorizationIntercepto ...

  9. 我的arcgis培训照片12

    来自:http://www.cioiot.com/successview-381-1.html

  10. CTO是有门槛的 我眼中真正优秀CTO应具备五大素质

    最近几个月,不断有人找我推荐CTO人选,这两年互联网创业和创投实在是太火爆了,全民创业,创业项目井喷,一下子发现CTO不够用了,全行业缺CTO,到处都在找CTO.说实话,我自己也没有CTO存货,CTO ...