以前写文件的复制很麻烦,需要各种输入流,然后读取line,输出到输出流…其实apache.commons.io里面提供了输入流输出流的常用工具方法,非常方便。下面就结合源码,看看IOUTils都有什么用处吧!
以前写文件的复制很麻烦,需要各种输入流,然后读取line,输出到输出流…其实apache.commons.io里面提供了输入流输出流的常用工具方法,非常方便。下面就结合源码,看看IOUTils都有什么用处吧!
常用的静态变量
在IOUtils中还是有很多常用的一些变量的,比如换行符等等1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public static final char DIR_SEPARATOR_UNIX = '/';
public static final char DIR_SEPARATOR_WINDOWS = '\\';
public static final char DIR_SEPARATOR;
public static final String LINE_SEPARATOR_UNIX = "\n";
public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
public static final String LINE_SEPARATOR;
static {
DIR_SEPARATOR = File.separatorChar;
StringBuilderWriter buf = new StringBuilderWriter(4);
PrintWriter out = new PrintWriter(buf);
out.println();
LINE_SEPARATOR = buf.toString();
out.close();
}
常用方法
copy
这个方法可以拷贝流,算是这个工具类中使用最多的方法了。支持多种数据间的拷贝:1
2
3
4
5
6copy(inputstream,outputstream)
copy(inputstream,writer)
copy(inputstream,writer,encoding)
copy(reader,outputstream)
copy(reader,writer)
copy(reader,writer,encoding)
copy内部使用的其实还是copyLarge方法。因为copy能拷贝Integer.MAX_VALUE的字节数据,即2^31-1。
copyLarge
这个方法适合拷贝较大的数据流,比如2G以上。1
2copyLarge(reader,writer) 默认会用1024*4的buffer来读取
copyLarge(reader,writer,buffer)
内部的细节可以参考:1
2
3
4
5
6
7
8
9public static long copyLarge(Reader input, Writer output, char [] buffer) throws IOException {
long count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
这个方法会用一个固定大小的Buffer,持续不断的读取数据,然后写入到输出流中。
read
从一个流中读取内容1
2
3
4
5
6read(inputstream,byte[])
read(inputstream,byte[],offset,length)
//offset是buffer的偏移值,length是读取的长度
read(reader,char[])
read(reader,char[],offset,length)
这里我写了个小例子,可以测试read方法的效果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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();
}
}
得到的结果是:1
2hell
□□hell□□□□
readFully
这个方法会读取指定长度的流,如果读取的长度不够,就会抛出异常1
2
3
4readFully(inputstream,byte[])
readFully(inputstream,byte[],offset,length)
readFully(reader,charp[])
readFully(reader,char[],offset,length)
比如:1
2
3
4
5
6
7
8
9
10
11
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();
}
}
输出1
hell
但是如果读取20个byte,就会出错了1
2
3
4
5java.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)
...
readLines
readLines方法可以从流中读取内容,并转换为String的list1
2
3
4readLines(inputstream)
readLines(inputstream,charset)
readLines(inputstream,encoding)
readLines(reader)
这个方法极大简化了之前原始的读取方法:1
2
3
4
5
6
7
8
9
10
11
12
13
14
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();
}
}
输出内容:1
2
3
4
5hello
world
nihao
ioutils
skip
这个方法用于跳过指定长度的流,1
2
3skip(inputstream,skip_length)
skip(ReadableByteChannel,skip_length)
skip(reader,skip_length)
例如:1
2
3
4
5
6
7
8
9
10
public void skipTest(){
InputStream is = IOUtils.toInputStream("hello world");
try {
IOUtils.skip(is,4);
System.out.println(IOUtils.toString(is,"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
skipFully
这个方法类似skip,只是如果忽略的长度大于现有的长度,就会抛出异常1
2
3skipFully(inputstream,toSkip)
skipFully(readableByteChannel,toSkip)
skipFully(inputstream,toSkip)
例如1
2
3
4
5
6
7
8
9
10
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();
}
}
write
这个方法可以把数据写入到输出流中1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19write(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)
例如1
2
3
4
5
6
7
8
9
10
11
public void writeTest(){
try {
OutputStream os = new FileOutputStream("E:/test.txt");
IOUtils.write("hello write!",os);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
writeLines
这个方法可以把string的List写入到输出流中1
2
3
4writeLines(Collection<?> lines, String lineEnding, OutputStream output)
writeLines(Collection<?> lines, String lineEnding, OutputStream output, Charset encoding)
writeLines(Collection<?> lines, String lineEnding, OutputStream output, String encoding)
writeLines(Collection<?> lines, String lineEnding, Writer writer)
例如1
2
3
4
5
6
7
8
9
10
public void writeLinesTest() throws IOException {
List<String> lines = new ArrayList();
lines.add("hello");
lines.add("list");
lines.add("to");
lines.add("file");
OutputStream os = new FileOutputStream("E:/test.txt");
IOUtils.writeLines(lines,IOUtils.LINE_SEPARATOR,os);
}
close
关闭URL连接1
close(URLConnection conn)
closeQuietly
忽略nulls和异常,关闭某个流1
2
3
4
5
6
7
8
9
10close(URLConnection conn)
closeQuietly(Closeable... closeables)
closeQuietly(Closeable closeable)
closeQuietly(InputStream input)
closeQuietly(OutputStream output)
closeQuietly(Reader input)
closeQuietly(Selector selector)
closeQuietly(ServerSocket sock)
closeQuietly(Socket sock)
closeQuietly(Writer output)
contentEquals
比较两个流是否相同1
2contentEquals(InputStream input1, InputStream input2)
contentEquals(Reader input1, Reader input2)
例如1
2
3
4
5
6
7
8
9
10
11
public void contentEqualsTest(){
InputStream is1 = IOUtils.toInputStream("hello123");
InputStream is2 = IOUtils.toInputStream("hello123");
try {
System.out.println(IOUtils.contentEquals(is1,is2));
} catch (IOException e) {
e.printStackTrace();
}
}
contentEqualsIgnoreEOL
比较两个流,忽略换行符1
contentEqualsIgnoreEOL(Reader input1, Reader input2)
lineIterator
读取流,返回迭代器1
2
3lineIterator(InputStream input, Charset encoding)
lineIterator(InputStream input, String encoding)
lineIterator(Reader reader)
toBufferedInputStream
把流的全部内容放在另一个流中1
2toBufferedInputStream(InputStream input)
toBufferedInputStream(InputStream input, int size)
toBufferedReader
返回输入流1
2toBufferedReader(Reader reader)
toBufferedReader(Reader reader, int size)
toByteArray
返回字节数组1
2
3
4
5
6
7
8
9
10toByteArray(InputStream input)
toByteArray(InputStream input, int size)
toByteArray(InputStream input, long size)
toByteArray(Reader input)
toByteArray(Reader input, Charset encoding)
toByteArray(Reader input, String encoding)
toByteArray(String input)
toByteArray(URI uri)
toByteArray(URL url)
toByteArray(URLConnection urlConn)
toCharArray
返回字符数组1
2
3
4toCharArray(InputStream is)
toCharArray(InputStream is, Charset encoding)
toCharArray(InputStream is, String encoding)
toCharArray(Reader input)
toInputStream
返回输入流1
2
3
4
5
6toInputStream(CharSequence input)
toInputStream(CharSequence input, Charset encoding)
toInputStream(CharSequence input, String encoding)
toInputStream(String input)
toInputStream(String input, Charset encoding)
toInputStream(String input, String encoding)
toString
返回字符串1
2
3
4
5
6
7
8
9
10
11
12toString(byte[] input)
toString(byte[] input, String encoding)
toString(InputStream input)
toString(InputStream input, Charset encoding)
toString(InputStream input, String encoding)
toString(Reader input)
toString(URI uri)
toString(URI uri, Charset encoding)
toString(URI uri, String encoding)
toString(URL url)
toString(URL url, Charset encoding)
toString(URL url, String encoding)