Java IO vs Java NIO

IO vs NIO tabular comparison Topic IO NIO 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 … Read more

Java IO – Pipes And Print Writer Next

Read from Console using pipes We see the use of a buffered reader and writer and a piped reader and writer. The example also demonstrates how to read from a console continuously. The example reads from a console using a buffered reader, passes the characters to a piped writer. A piped reader reads the characters … Read more

Java NIO – Channels and Memory mapping

Channel Classes Channel– A channel represents a connection to a file, socket or any other component that can perform IO operations. A channel reads or writes data to a byte buffer. Channels are generally thread safe. A channel may be specified to be read only or both readable and writable. ReadableByteChannel – A ReadableByteChannel permits … Read more

Java IO – RandomAccessFiles

Introduction A Random access file behaves like an array of bytes. It can be used to both read and write bytes. RandomAccessFile allows to read or write data at a particular position. It implements the DataInput and DataOutput interfaces and hence allows reading and writing java primitive types. The file can be opened in both … Read more

Java IO – Read and Write Java Objects

Introduction ObjectInputStream can be used to de-serialize and read java objects that have been serialized and written using ObjectOutputStream. In the example below the ‘Course’ object is written to a persistent storage. The Course object contains a list of Student objects. The object writing preserves object references. We first create an object output stream that … Read more

Java IO – Read and write text files

Reading Character Streams – Classes All classes that read character stream extend java.io.Reader. The important classes are: InputStreamReader-This class reads bytes from an underlying stream and converts it to characters using a Charset. It acts as a bridge between a byte stream and character stream PipedReader- This class connects to a PipedWriter and reads characters … Read more

Java IO – Read and Write Bytes

Reading Bytes – classes The basic class in java.io to read data is java.io.InputStream. All classes that read bytes are derived from this class. This class is extended by the following classes. Detailed examples follow later, but lets take a 10000 feet view of the classes. We will look only at the most important classes. … Read more

Java NIO – Watch a directory

Java 7 implemented the watch service which can be used to watch changes in a directory. The watch service is thread safe and hence multiple threads can use it. Here’s a complete example of the watch service. All steps are explained in the example

Java NIO – Buffers

Buffers are containers of data. They store bytes and other primitive java types in arrays and allow processes to read and write bytes onto them. Buffers have three important properties. 1. Position – identifies the index where the next byte will be written or read. 2. Limit – identifies the index where there is no … Read more