api: http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html
方法:
- copy:拷贝流。
- copyLarge:拷贝较大的数据流。
- read:从一个流中读取内容。
- readFully:读取指定长度的流。
- readLines:
- contentEquals:比较两个流是否相等。
- contentEqualsIgnoreEOL:比较两个流,忽略换行符。
- skip:跳过指定长度的流。
- skipFully:类似skip,如果忽略的长度大于现有的长度,抛出异常。
- write:把数据写入到输出流中。
- writeLines:
- toBufferedInputStream: 把流的全部内容放在另一个流中。
- toBufferedReader: 返回输入流,。
- toByteArray: 返回字节数组。
- toCharArray: 返回字符数组。
- toInputStream: 返回输入流。
- toString: 返回字符串。
- ineIterator:读取流,返回迭代器。
- close:关闭流。
jar 包 gradle项目
// https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils
compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.3'
// https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils-core
compile group: 'commons-beanutils', name: 'commons-beanutils-core', version: '1.8.3'
// https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils-bean-collections
compile group: 'commons-beanutils', name: 'commons-beanutils-bean-collections', version: '1.8.3'
// https://mvnrepository.com/artifact/log4j/log4j
compile group: 'log4j', name: 'log4j', version: '1.2.17'
包
import org.apache.commons.io.IOUtils;
常用的静态变量
在IOUtils中有很多常用的一些变量的,比如换行符等等
// NOTE: This class is focussed on InputStream, OutputStream, Reader and
// Writer. Each method should take at least one of these as a parameter,
// or return one of them.
private static final int EOF = -1;
/**
* The Unix directory separator character.
*/
public static final char DIR_SEPARATOR_UNIX = '/';
/**
* The Windows directory separator character.
*/
public static final char DIR_SEPARATOR_WINDOWS = '\\';
/**
* The system directory separator character.
*/
public static final char DIR_SEPARATOR = File.separatorChar;
/**
* The Unix line separator string.
*/
public static final String LINE_SEPARATOR_UNIX = "\n";
/**
* The Windows line separator string.
*/
public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
/**
* The system line separator string.
*/
public static final String LINE_SEPARATOR;
static {
// avoid security issues
StringBuilderWriter buf = new StringBuilderWriter(4);
PrintWriter out = new PrintWriter(buf);
out.println();
LINE_SEPARATOR = buf.toString();
out.close();
}
/**
* The default buffer size ({@value}) to use for
* {@link #copyLarge(InputStream, OutputStream)}
* and
* {@link #copyLarge(Reader, Writer)}
*/
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
/**
* The default buffer size to use for the skip() methods.
*/
private static final int SKIP_BUFFER_SIZE = 2048;
常用方法
-
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)
// copy from InputStream
//-----------------------------------------------------------------------
/**
* Copy bytes from an <code>InputStream</code> to an
* <code>OutputStream</code>.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
* Large streams (over 2GB) will return a bytes copied value of
* <code>-1</code> after the copy has completed since the correct
* number of bytes cannot be returned as an int. For large streams
* use the <code>copyLarge(InputStream, OutputStream)</code> method.
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @return the number of bytes copied, or -1 if > Integer.MAX_VALUE
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static int copy(InputStream input, OutputStream output) throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
/**
* 直接把输入流关联的文件,通过输入流输出到文件中
* 输入流不需要带缓冲(BufferedInputStream),内置缓冲
*/
@Test
public void test3() throws IOException {
InputStream inputStream = new FileInputStream("F:/downloadFromHDFS.txt");
OutputStream outputStream = new FileOutputStream("F:/Test/out.txt");
IOUtils.copy(inputStream,outputStream);
}
注释:2G以上就不要用上面的方法了,copy内部使用的其实还是copyLarge方法。因为copy能拷贝Integer.MAX_VALUE 的字节数据,即2^31-1。
- copyLarge:这个方法适合拷贝较大的数据流,比如2G以上。
copyLarge(reader,writer) //默认会用1024*4的buffer来读取
copyLarge(reader,writer,buffer)
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
/**
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @return the number of bytes copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 1.3
*/
public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
}
/**
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>.
* <p>
* This method uses the provided buffer, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @param buffer the buffer to use for the copy
* @return the number of bytes copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(InputStream input, OutputStream output, byte[] 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,持续不断的读取数据,然后写入到输出流中。
- 获取输入流
//通过文本获取输入流 , 可以指定编码格式
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)
- 字符串读写
//readLines方法可以从流中读取内容,并转换为String的list
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)
//writeLines:这个方法可以把string的List写入到输出流中
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)
这个方法极大简化了之前原始的读取方法:
/**
* 一次读取一行存入 List<String> 里面
*/
@Test
public void readLinesTest() throws IOException {
InputStream is = new FileInputStream("F:/Test/1.cpp");
List<String> lines = IOUtils.readLines(is);
System.out.println(lines.size());
for (String line : lines) {
System.out.println(line);
}
}
@Test
public void readLinesTest() throws IOException {
InputStream is = new FileInputStream("F:/Test/out.txt");
List<String> lines = IOUtils.readLines(is);
for (String line : lines) {
System.out.println(line);
}
}
/**
* 一个元素一行,写入文件
*/
@Test
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("F:/Test/out.txt");
IOUtils.writeLines(lines,IOUtils.LINE_SEPARATOR,os);
}
- 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)
@Test
public void writeTest() throws IOException {
OutputStream os = new FileOutputStream("F:/Test/out.txt");
IOUtils.write("hello write!", os);//把 hello write! 写入文件 out.txt
}
- read:从一个流中读取内容
read(inputstream,byte[])
read(inputstream,byte[],offset,length)
//offset是buffer的偏移值,length是读取的长度
read(reader,char[])
read(reader,char[],offset,length)
@Test
public void test4() throws IOException {
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);//重bytes 数组下标为2开始填充到4
System.out.println(new String(bytes));
}
- readFully:这个方法会读取指定长度的流,如果读取的长度不够,就会抛出异常
readFully(inputstream,byte[])
readFully(inputstream,byte[],offset,length)
readFully(reader,charp[])
readFully(reader,char[],offset,length)
@Test
public void readFullyTest() throws IOException {
byte[] bytes = new byte[4];
InputStream is = IOUtils.toInputStream("hello world");
IOUtils.readFully(is, bytes);
System.out.println(new String(bytes));
byte[] bytes2 = new byte[20];//读取的长度不够,就会抛出异常
InputStream inputStream = IOUtils.toInputStream("hello world");
IOUtils.readFully(inputStream, bytes2);
System.out.println(new String(bytes2));
}
hell
java.io.EOFException: Length to read: 4 actual: 2
at org.apache.commons.io.IOUtils.readFully(IOUtils.java:2539)
at org.apache.commons.io.IOUtils.readFully(IOUtils.java:2558)
at com.bug.apache.Utils.IOUtilsDemo.Main.readFullyTest(Main.java:59)
- contentEquals:比较两个流是否相等
contentEquals(InputStream input1, InputStream input2)
contentEquals(Reader input1, Reader input2)
@Test
public void contentEqualsTest() throws IOException {
InputStream inputStream = new FileInputStream("F:/downloadFromHDFS.txt");
InputStream inputStream2 = new FileInputStream("F:/Test/out.txt");//前面复制的文件
InputStream inputStream3 = new FileInputStream("F:/Test/Main.java");
System.out.println(IOUtils.contentEquals(inputStream, inputStream2));//两个文件相同,将返回 true
System.out.println(IOUtils.contentEquals(inputStream, inputStream3));//两个文件不同,将返回 false
}
@Test
public void contentEqualsTest2() throws IOException {
InputStream is1 = IOUtils.toInputStream("hello123");
InputStream is2 = IOUtils.toInputStream("hello123");
System.out.println(IOUtils.contentEquals(is1,is2));//true
}
- contentEqualsIgnoreEOL:比较两个流,忽略换行符
contentEqualsIgnoreEOL(Reader input1, Reader input2)
- skip:这个方法用于跳过指定长度的流
long skip(inputstream,skip_length)
long skip(ReadableByteChannel,skip_length)
long skip(reader,skip_length)
@Test
public void skipTest() throws IOException {
InputStream is = IOUtils.toInputStream("hello world");
IOUtils.skip(is, 4);
System.out.println(IOUtils.toString(is, "utf-8"));//o world
}
- skipFully:这个方法类似skip,只是如果忽略的长度大于现有的长度,就会抛出异常。
skipFully(inputstream,toSkip)
skipFully(readableByteChannel,toSkip)
skipFully(inputstream,toSkip)
@Test
public void skipFullyTest() throws IOException {
InputStream is = IOUtils.toInputStream("hello world");
IOUtils.skipFully(is, 30);//跳过的长度大于了先用的长度
System.out.println(IOUtils.toString(is, "utf-8"));
}
- toBufferedInputStream:把流的全部内容放在另一个流中
toBufferedInputStream(InputStream input)
toBufferedInputStream(InputStream input, int size)
- toBufferedReader:返回输入流
toBufferedReader(Reader reader)
toBufferedReader(Reader reader, int size)
- toByteArray:返回字节数组
toByteArray(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:返回字符数组
toCharArray(InputStream is)
toCharArray(InputStream is, Charset encoding)
toCharArray(InputStream is, String encoding)
toCharArray(Reader input)
- toInputStream:返回输入流
toInputStream(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:返回字符串
toString(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)
- ineIterator:读取流,返回迭代器
LineIterator lineIterator(InputStream input, Charset encoding)
LineIterator lineIterator(InputStream input, String encoding)
LineIterator lineIterator(Reader reader)
- close:关闭流
//关闭URL连接 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)
@Test
public void test5() throws IOException {
String meString = "哈哈哈哈,下班了,hello";
InputStream inputStream = IOUtils.toInputStream(meString, "utf-8");
String mes = IOUtils.toString(inputStream, "utf-8");
System.out.println(mes);
}
/**
* 模拟http 请求
*/
@Test
public void test6() throws IOException {
String meString = "http://www.baidu.com";
//模拟了http 请求
String mes = IOUtils.toString(new URL(meString), "utf-8");
System.out.println(mes);
}
参考:
本文详细介绍了Apache Commons IOUtils类的功能及使用方法,包括流的拷贝、读取、写入等操作,提供了丰富的示例代码帮助理解。

2147

被折叠的 条评论
为什么被折叠?



