Doc Site
https://commons.apache.org/proper/commons-io/
javadoc
https://commons.apache.org/proper/commons-io/apidocs/index.html
usage
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
IOUtils
General IO stream manipulation utilities.
This class provides static utility methods for input/output operations.
- [Deprecated] closeQuietly - these methods close a stream ignoring nulls and exceptions
- toXxx/read - these methods read data from a stream
- write - these methods write data to a stream
- copy - these methods copy all the data from one stream to another
- contentEquals - these methods compare the content of two streams
The byte-to-char methods and char-to-byte methods involve a conversion step. Two methods are provided in each case, one that uses the platform default encoding and the other which allows you to specify an encoding. You are encouraged to always specify an encoding because relying on the platform default can lead to unexpected results, for example when moving from development to production.
All the methods in this class that read a stream are buffered internally. This means that there is no cause to use a BufferedInputStream
or BufferedReader
. The default buffer size of 4K has been shown to be efficient in tests.
The various copy methods all delegate the actual copying to one of the following methods:
copyLarge(InputStream, OutputStream, byte[\])
copyLarge(InputStream, OutputStream, long, long, byte[\])
copyLarge(Reader, Writer, char[\])
copyLarge(Reader, Writer, long, long, char[\])
For example, copy(InputStream, OutputStream)
calls copyLarge(InputStream, OutputStream)
which calls copy(InputStream, OutputStream, int)
which creates the buffer and calls copyLarge(InputStream, OutputStream, byte[\])
.
Applications can re-use buffers by using the underlying methods directly. This may improve performance for applications that need to do a lot of copying.
Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams’ origin and further use. Thus the caller is still responsible for closing streams after use.
javadoc
https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html
将行集合写入输出流
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("D:/aaa.txt");
List<String> lines = new ArrayList<>();
lines.add("aaaaaaaa");
lines.add("bbbbbbbb");
lines.add("cccccccc");
IOUtils.writeLines(lines, System.lineSeparator(), os, StandardCharsets.UTF_8.displayName());
os.flush();
os.close();
}
拷贝输入流到输出流
public static void main(String[] args) throws IOException {
// 拷贝输入流到输出流
InputStream inputStream = new FileInputStream("D:/src.txt");
OutputStream outputStream = new FileOutputStream("D:/dest.txt");
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.flush();
outputStream.close();
}
关闭流
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("D:/src.txt");
OutputStream outputStream = new FileOutputStream("D:/dest.txt");
IOUtils.copy(inputStream, outputStream);
outputStream.flush();
IOUtils.closeQuietly(inputStream,outputStream);
}
输入流转换为byte数组
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("D:/src.txt");
byte[] bytes = IOUtils.toByteArray(inputStream);
for (byte b : bytes) {
System.out.println(b);
}
IOUtils.closeQuietly(inputStream);
}
输入流转换为字符串
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream("D:/src.txt");
String s = IOUtils.toString(inputStream, StandardCharsets.UTF_8.displayName());
System.out.println(s);
IOUtils.closeQuietly(inputStream);
}
}
字符串转换为输入流
在 Java 中使用 ByteArrayInputStream() 把一个字符串转换为 InputStream
使用 StringReader 和 ReaderInputStream 将字符串转换为 InputStream
使用 org.apache.commons.io.IOUtils 将字符串转换为 InputStream
Java 的 Input/Output 包中有一个类 ByteArrayInputStream,可以将字节数组读取为 InputStream。首先,我们使用 getBytes() 从 exampleString 中获取字符集为 UTF_8 的字节,然后传递给 ByteArrayInputStream。
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws IOException {
String exampleString = "This is a sample string";
InputStream inputStream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
//To check if we can read the string back from the inputstream
int i;
while ((i = inputStream.read()) != -1){
char getSingleChar = (char)i;
System.out.print(getSingleChar);
}
}
}
使用 StringReader 和 ReaderInputStream 将字符串转换为 InputStream
将字符串转换为 InputStream 的第二种技术使用两个方法,即 StringReader 和 ReaderInputStream。前者用于读取字符串并将其包入 reader 中,而后者则需要两个参数,一个 reader 和 charsets。最后,我们得到 InputStream。
import org.apache.commons.io.input.ReaderInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws IOException {
String exampleString = "This is a sample string";
StringReader stringReader = new StringReader(exampleString);
InputStream inputStream = new ReaderInputStream(stringReader, StandardCharsets.UTF_8);
//To check if we can read the string back from the inputstream
int i;
while ((i = inputStream.read()) != -1){
char getSingleChar = (char)i;
System.out.print(getSingleChar);
}
}
}
使用 org.apache.commons.io.IOUtils 将字符串转换为 InputStream
我们还可以使用 Apache Commons 库来使我们的任务变得更简单。这个 Apache Commons 库的 IOUtls 类有一个 toInputStream() 方法,它接受一个字符串和要使用的 charset。这个方法是最简单的,因为我们只需要调用一个方法就可以将 Java 字符串转换为 InputStream。
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws IOException {
String exampleString = "This is a sample string";
InputStream is = IOUtils.toInputStream(exampleString, StandardCharsets.UTF_8);
//To check if we can read the string back from the inputstream
int i;
while ((i = is.read()) != -1){
char getSingleChar = (char)i;
System.out.print(getSingleChar);
}
}
}