转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/55051917
本文出自【赵彦军的博客】
在上一篇文章中,介绍了IO FileUtils的常规用法,今天介绍IOUtils
的使用。
另外我已经把Commons IO 2.5
的源码发布到Jcenter,大家就不用下载jar包了,可以直接引用。
<dependency>
<groupId>org.apache.commons.iogroupId>
<artifactId>commonsIOartifactId>
<version>2.5.0version>
<type>pomtype>
dependency>
compile 'org.apache.commons.io:commonsIO:2.5.0'
IOUtils
流操作的相关方法copy(inputstream,outputstream)
copy(inputstream,writer)
copy(inputstream,writer,encoding)
copy(reader,outputstream)
copy(reader,writer)
copy(reader,writer,encoding)
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 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(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(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(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(InputStream input1, InputStream input2)
contentEquals(Reader input1, Reader input2)
contentEqualsIgnoreEOL(Reader input1, Reader input2)
long skip(inputstream,skip_length)
long skip(ReadableByteChannel,skip_length)
long skip(reader,skip_length)
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();
}
}
LineIterator lineIterator(InputStream input, Charset encoding)
LineIterator lineIterator(InputStream input, String encoding)
LineIterator lineIterator(Reader reader)
//关闭 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)
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
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();
}
}
}
个人微信号:zhaoyanjun125
, 欢迎关注