Home » Core Java » nio

nio

Java Nio Write File Example

With this example we are going to demonstrate how to use the Non-blocking I/O API, or NIO.2 API (NIO API) for short, to write data to a file. The examples in this article are compiled and run in a Mac OS unix environment. Please note that Java SE 8 is required to run the code in this article. 1. Introduction to ...

Read More »

Java Nio SSL Example

This is an example of a non-blocking I/O provided by java.nio using SSL handshake. 1. Definition of Secure Sockets Layer Protocol (SSL) SSL is the secure communication protocol of choice for a large part of the Internet community. There are many applications of SSL in existence, since it is capable of securing any transmission over TCP. Secure HTTP, or HTTPS, ...

Read More »

Java Nio Socket Example

This article introduces the SocketChannel class and its basic usage. This class is defined in the java.nio package. 1. Standard Java sockets Socket programming involves two systems communicating with one another. In implementations prior to NIO, Java TCP client socket code is handled by the java.net.Socket class. A socket is one end-point of a two-way communication link between two programs ...

Read More »

java.nio.channels.spi.SelectorProvider Example

SelectorProvider is an abstract class defined in the java.nio.channels.spi package. This is a central service-provider class for selectors and selectable channels defined in the java.nio.channels API. A selector provider is a concrete subclass of this class that has a zero-argument constructor and implements the abstract factory methods of this class that return open channels and selector objects. A Java virtual ...

Read More »

java.nio.Buffer Example

This article introduces the Buffer class and its basic usage. This class is defined in the java.nio package. A buffer is a container for a fixed amount of data of a specific primitive type. There is one subclass of this class for each primitive type, except boolean. They are ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer and ShortBuffer classes. These are ...

Read More »

java.nio.channels.Selector Example

This example shows the basic usage of Selector. This is an abstract class defined in the java.nio.channels package. Selector is a multiplexor of SelectableChannel objects. NOTE: From the thefreedictionary.com a data-multiplexer is defined as a multiplexer that permits two or more data sources to share a common transmission medium. Multiplexed, non-blocking I/O, which is much more scalable than thread-oriented, blocking ...

Read More »

Java Direct ByteBuffer Example

This example shows the usage of direct ByteBuffer. ByteBuffer is an abstract class, extends Buffer and implements Comparable<ByteBuffer>. This class is defined in the java.nio package. A buffer is a container for a fixed amount of data of a specific primitive type. There is a buffer class for each non-boolean primitive type. A ByteBuffer is a sub class of Buffer ...

Read More »

java.nio.channels.ScatteringByteChannel Example

ScatteringByteChannel is an interface extends ReadableByteChannel and is defined in java.nio.channels package. This is a channel that can read bytes into a sequence of buffers. This interface defines two read methods. read(ByteBuffer [] dsts): Reads a sequence of bytes from this channel into the given buffers. read(ByteBuffer [] dsts, int offset, int length): Reads a sequence of bytes from this ...

Read More »

java.nio.channels.FileLock Example

This article introduces the FileLock class and its basic usage. This article examples show using file locks with FileChannels. The examples in this article are compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code. 1. Introduction FileLock is a token representing a lock on a region of a file. This is ...

Read More »

java.nio.channels.AsynchronousChannelGroup Example

This article introduces the AsynchronousChannelGroup and its basic usage. This class is available since Java SE 7 as part of Java NIO 2 file API. This article’s example shows using this class with asynchronous channels. The example in this article is compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code. 1. ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns