We have launched a free Basic Kubernetes Course. Click here to enrol now! Kubernetes is a container orchestration platform used by enterprises worldwide and Kubernetes developers are in high demand!
IO vs NIO tabular comparison
|Reading/Writing||Provides classes to read and write bytes and characters from files and sockets. The reading and writing can be buffered. Easy to use.||Provides channels that interface with files or sockets to manage data. There is no specific advantage to using NIO over IO.|
|Large Files||All files need to be loaded in the JVM, hence handling large files may be difficult.||Memory Mapped Buffers allow mapping a file directly from the file system (without loading into memory). It would be possible to handle very large files without running out of heap space.|
|Threads and Blocking||IO is blocking in nature. For example, if you open a socket connection, a dedicated thread is required to handle the socket client.||NIO can be implemented in non blocking fashion. Using the socket example, a selector framework selects a socket client when it has data available. The availability notification may be provided by the File System.|
|Copying||Copying is accomplished by reading from a file into the jvm and writing back to another file.||Copying can be accomplished by directly transferring data from one channel to another and therefore copying large files may be faster (not guaranteed though).|
|Scatter/Gather||Not Available||NIO provides classes and methods to read from a channel into multiple buffers in a single operation and also write from multiple buffers into a single channel|
|File Operations||IO provides the ‘File’ class that represents the actual file.||NIO provides the ‘Path’ class that holds the path to the actual file. Path combined with the java.nio.file.Files class provides a lot of powerful functionalities that include walking a directory tree.|
Apache Commons really does not compete with IO or NIO but can be thought of as a collection of convenience classes that provide wrapper methods for common functionalities. The FileUtils and the IOUtils are probably the most important classes in apache commons IO. They provide a group of methods that include reading from a file into a list of string, reading from a file into a String, reading from a URL into a string, copying files, directory listing using Filters etc. Apache commons has a lot of filters that provide off the shelf functionality. Filters can the combined to create complex filters. For example, filters can be combined to list files from directory starting with ‘X’ or ‘Y’ and files ending with ‘.txt’ or ‘.xls’.