IO
IOothers-IoUtil

IOothers-IoUtil

Origins

The existence of IO tool classes mainly aims to simplify the encapsulation of InputStream, OutputStream, Reader, and Writer, and to encapsulate简化 NIO-related operations. Overall, Hutool’s encapsulation of IO is mainly at the tool level, and we strive to find the best balance between convenience, performance, and flexibility.

Methods

Copy

The process of reading from an input stream and writing to an output stream can be summarized as copy. This is a basic process and also the foundation of file and stream operations.

For example, copying a file stream:

BufferedInputStream in = FileUtil.getInputStream("d:/test.txt");
BufferedOutputStream out = FileUtil.getOutputStream("d:/test2.txt");
long copySize = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE);

The copy method also has some overloaded methods for objects such as Reader, Writer, Channel, etc., and provides optional cache sizes. The default cache size is 1024 bytes. If copying large files or streams involves a large amount of data, the parameter can be adjusted accordingly.

For NIO, a copyByNIO method is provided to distinguish it from BIO. I have read some materials and found that using NIO for file stream operations can improve performance. I did not conduct any specific experiments myself. Please refer to the relevant test in the blog: http://www.cnblogs.com/gaopeng527/p/4896783.html

Stream to Reader、Writer

  • IoUtil.getReader: Convert InputStream to BufferedReader for reading character streams, which is the foundation of some partial readXXX methods.
  • IoUtil.getWriter: Convert OutputStream to OutputStreamWriter for writing character streams, which is the foundation of some partial writeXXX methods.

Essentially, these two methods simply create a new Reader or Writer object, but encapsulating them as tool methods and IDE auto-suggestions can greatly reduce the need for lookup (for example, if you are not familiar with BufferedReader or OutputStreamWriter, do you need to search for related classes?).

Reading Content from Streams

Reading content from streams can be summarized as read methods and readXXX methods.

  1. The read method has numerous overloaded methods that can read content from different objects, including:
  • InputStream
  • Reader
  • FileChannel

These three overloaded methods mostly return a String string, providing great convenience for reading character streams.

  1. The readXXX methods mainly handle some processing for the return values, such as:
  • readBytes returns a byte array (for reading images, etc.)
  • readHex reads hexadecimal strings
  • readObj reads serialized objects (deserialization)
  • readLines reads content line by line
  1. The toStream method converts some objects into stream objects for easier manipulation in some cases:
  • String is converted into ByteArrayInputStream
  • File is converted into FileInputStream

Writing to Streams

  • The IoUtil.write method has two overloaded methods: one directly calls OutputStream.write method, and the other is used to convert an object to a string (by calling the toString method) and write it to a stream.
  • The IoUtil.writeObjects method is used to serialize and write serializable objects to a stream.

The write method does not provide writeXXX methods, so you need to convert them to a String or byte[] yourself.

Closure

For IO operations, the most frequently used (and easily forgotten) operation is closure using the close method. Fortunately, the Java specification uses the elegant Closeable interface, so we simply need to encapsulate this interface’s method calls.

Closure faces two problems:

  1. The closed object is null.
  2. The object closure fails or the object has already been closed.

The IoUtil.close method effectively solves these two problems.

In JDK1.7, an AutoCloseable interface is provided, and an overloaded method is also provided in IoUtil for ease of use with no difference observed during usage.