{"id":87426,"date":"2019-02-08T13:05:48","date_gmt":"2019-02-08T11:05:48","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?page_id=87426"},"modified":"2023-03-06T16:04:03","modified_gmt":"2023-03-06T14:04:03","slug":"java-nio-tutorials","status":"publish","type":"page","link":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials","title":{"rendered":"Java NIO Tutorials"},"content":{"rendered":"<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\"><img decoding=\"async\" class=\"aligncenter wp-image-62\" style=\"border: none;\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" alt=\"Java NIO Tutorials\" width=\"300\" height=\"300\"><\/a><\/p>\n<p>In this detailed Resource page, we feature an abundance of <strong>Java NIO Tutorials<\/strong>!<\/p>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Non-blocking_I\/O_(Java)\">java.nio<\/a> (NIO stands for Non-blocking I\/O) is a collection of Java programming language APIs that offer features for intensive I\/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I\/O. NIO was developed under the Java Community Process as JSR 51. An extension to NIO that offers a new file system API, called NIO.2, was released with Java SE 7 (&#8220;Dolphin&#8221;).<\/p>\n<h2>Features and organization<\/h2>\n<p>The APIs of NIO were designed to provide access to the low-level I\/O operations of modern operating systems. Although the APIs are themselves relatively high-level, the intent is to facilitate an implementation that can directly use the most efficient operations of the underlying platform.<\/p>\n<p>The Java NIO APIs are provided in the java.nio package and its subpackages. The documentation by Oracle identifies these features.<\/p>\n<ul>\n<li>Buffers for data of primitive types<\/li>\n<li>Character set encoders and decoders<\/li>\n<li>A pattern-matching facility based on Perl-style regular expressions (in package java.util.regex)<\/li>\n<li>Channels, a new primitive I\/O abstraction<\/li>\n<li>A file interface that supports locks and memory mapping of files up to Integer.MAX_VALUE bytes (2 GB)<\/li>\n<li>A multiplexed, non-blocking I\/O facility for writing scalable servers<\/li>\n<\/ul>\n<h2>NIO buffers<\/h2>\n<p>NIO data transfer is based on buffers (java.nio.Buffer and related classes). These classes represent a contiguous extent of memory, together with a small number of data transfer operations. Although theoretically these are general-purpose data structures, the implementation may select memory for alignment or paging characteristics, which are not otherwise accessible in Java. Typically, this would be used to allow the buffer contents to occupy the same physical memory used by the underlying operating system for its native I\/O operations, thus allowing the most direct transfer mechanism, and eliminating the need for any additional copying. In most operating systems, provided the particular area of memory has the right properties, transfer can take place without using the CPU at all. The NIO buffer is intentionally limited in features in order to support these goals.<\/p>\n<p>There are buffer classes for all of Java&#8217;s primitive types except boolean, which can share memory with byte buffers and allow arbitrary interpretation of the underlying bytes.<\/p>\n<h2>Usage<\/h2>\n<p>NIO buffers maintain several pointers that dictate the function of their accessor methods. The NIO buffer implementation contains a rich set of methods for modifying these pointers:<\/p>\n<ul>\n<li>The <code>flip()<\/code> method, rather than performing a &#8220;flip&#8221; or paging function in the canonical sense, moves the position pointer to the origin of the underlying array (if any) and the limit pointer to the former position of the position pointer.<\/li>\n<li>Three <code>get()<\/code> methods are supplied for transferring data out of a NIO buffer. The bulk implementation, rather than performing a &#8220;get&#8221; in the traditional sense, &#8220;puts&#8221; the data into a specified array. The &#8220;offset&#8221; argument supplied to this method refers not to the offset from within the buffer from which to read, nor an offset from the position pointer, but rather the offset from 0 within the target array.<\/li>\n<li>Unless using the absolute <code>get()<\/code> and <code>put()<\/code> methods, any <code>get()<\/code> or <code>put()<\/code> is conducted from the position pointer. Should one need to read from a different position within the underlying array, whilst not adjusting the writing position, the <code>mark()<\/code> and <code>reset()<\/code> methods have been supplied.<br \/>\nThe <code>mark()<\/code> method effectively stores the position of the position pointer by setting the mark pointer to the position of the position pointer. The <code>reset()<\/code> method causes the position pointer to move to the mark pointer&#8217;s position.<br \/>\nUpon invocation of the <code>clear()<\/code> method or the <code>flip()<\/code> method the mark pointer is discarded.<\/li>\n<li>The <code>clear()<\/code> method does not ensure zero-ing of the buffer, but does return the limit pointer to the upper boundary of the underlying array, and the position pointer to <code>zero.put()<\/code> and <code>get()<\/code> operations for NIO buffers are not thread safe.<\/li>\n<li>You can only <code>map()<\/code> a <code>java.nio.MappedByteBuffer<\/code> from a <code>java.nio.channels.FileChannel<\/code> up to <code>Integer.MAX_VALUE<\/code> in size (2GiB); regions beyond this limit can be accessed using an offset greater than zero.<\/li>\n<\/ul>\n<h2>Channels<\/h2>\n<p>Channels (classes implementing the interface <code>java.nio.channels.Channel<\/code>) are designed to provide for bulk data transfers to and from NIO buffers. This is a low-level data transfer mechanism that exists in parallel with the classes of the higher-level I\/O library (packages <code>java.io<\/code> and <code>java.net<\/code>). A channel implementation can be obtained from a high-level data transfer class such as <code>java.io.File<\/code>, <code>java.net.ServerSocket<\/code>, or java.net.Socket, and vice versa. Channels are analogous to &#8220;file descriptors&#8221; found in Unix-like operating systems.<\/p>\n<p>File channels (<code>java.nio.channels.FileChannel<\/code>) can use arbitrary buffers but can also establish a buffer directly mapped to file contents using memory-mapped file. They can also interact with file system locks. Similarly, socket channels (<code>java.nio.channels.SocketChannel<\/code> and <code>java.nio.channels.ServerSocketChannel<\/code>) allow for data transfer between sockets and NIO buffers.<\/p>\n<h2>Selectors<\/h2>\n<p>A selector (<code>java.nio.channels.Selector<\/code> and sub-classes) provides a mechanism for waiting on channels and recognizing when one or more become available for data transfer. When a number of channels are registered with the selector, it enables blocking of the program flow until at least one channel is ready for use, or until an interruption condition occurs.<\/p>\n<p>Although this multiplexing behavior could be implemented with threads, the selector can provide a significantly more efficient implementation using lower-level operating system constructs. A POSIX-compliant operating system, for example, would have direct representations of these concepts, <code>select()<\/code>. A notable application of this design would be the common paradigm in server software which involves simultaneously waiting for responses on a number of sessions.<\/p>\n<h2>Character sets<\/h2>\n<p>In Java, a character set is a mapping between Unicode characters (or a subset of them) and bytes. The <code>java.nio.charset<\/code> package of NIO provides facilities for identifying character sets and providing encoding and decoding algorithms for new mappings.<\/p>\n<h2>Reception<\/h2>\n<p>Unexpected is that a Channel associated to a Java IO RandomAccess file closes the file descriptor on an interrupt. While RandomAccessFiles own read method does not do this.<\/p>\n<div class=\"tip\"><strong>Note<\/strong><br \/>\nIf you wish to build up your Java NIO knowledge first, check out our <a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-tutorial-beginners\/\">Java Nio Tutorial for Beginners<\/a>.<\/div>\n<p>[ulp id=&#8217;R6CIBqQwEyVH8p3P&#8217;]<\/p>\n<h2>Java NIO Tutorials &#8211; Getting Started<\/h2>\n<h3>Simple examples based on Java NIO<\/h3>\n<ul>\n<li><a href=\"https:\/\/www.javacodegeeks.com\/2018\/07\/java-nio-tutorial.html\">Java NIO Tutorial<\/a><br \/>\nJava NIO is a library introduced from Java 1.4. Java NIO has since its launch provided an alternative way to handle I\/O and networking transactions. It is considered to be an alternative to Java Networking and Java IO libraries.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-echo-server-tutorial\/\">Java Nio Echo Server Tutorial<\/a><br \/>\nThis article is a tutorial on implementing a simple Java NIO \u201cecho server\u201d. This example will take the form of a rather simple client server application whereby a client or many clients will connect to a running server and post message(s) to the server which will in turn be \u201cechoed\u201d back to the respective clients.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-async-http-client-example\/\">Java Nio Async HTTP Client Example<\/a><br \/>\nThis article is an example of how to build a simple asynchronous Http client using Java Nio. This example will make use of the httpbin service for much of it\u2019s test cases, which can also be verified via postman or curl. Although the examples work, this is by no means a production ready.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-create-directory-example\/\">Java Nio Create Directory Example<\/a><br \/>\nJava NIO was developed to allow the Java programmers implement the high-speed input-output operations without using the custom native code. NIO moves the time-taking I\/O activities like filling, namely and draining buffers etc back into the operating system, thus allows the great increase in the operational speed.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-ftp-example\/\">Java Nio FTP Example<\/a><br \/>\nIn this example we will demonstrate an FTP example program written in Java using some of the NIO features available to us. We will configure our local operating system to serve a specific directory via FTP. In order to simulate the FTP server we will make use of the vsftpd ubuntu package.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-heartbeat-example\/\">Java Nio Heartbeat Example<\/a><br \/>\nThis article is a tutorial on implementing a simple Java NIO Heartbeat. This example will take the form of \u201cn\u201d number of \u201cBroadcast\u201d mode processes which will multicast data via UDP to \u201cn\u201d number of \u201cSubscribe\u201d processes that have expressed interest in receiving said traffic.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-ssl-example\/\">Java Nio SSL Example<\/a><br \/>\nThis is an example of a non-blocking I\/O provided by java.nio using SSL handshake. 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.<\/li>\n<\/ul>\n<h2>Java NIO Tutorials &#8211; Functions<\/h2>\n<h3>Learn the most famous functionalities and operations of the Java NIO<\/h3>\n<h4>File<\/h4>\n<ul>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-large-file-transfer-tutorial\/\">Java Nio Large File Transfer Tutorial<\/a><br \/>\nThis article is a tutorial on transferring a large file using Java Nio. It will take shape via two examples demonstrating a simple local file transfer from one location on hard disk to another and then via sockets from one remote location to another remote location.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-iterate-files-directory\/\">Java Nio Iterate Over Files in Directory<\/a><br \/>\nThis example will demonstrate iterating over the files and directories recursively from a root directory. The example program will utilize some of the Java NIO features. Central to this example are the DirectoryStream, Filter, Path and Paths classes.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-append-file-example\/\">Java Nio Append File Example<\/a><br \/>\nJava NIO (i.e. new I\/O) is an interesting file input-output mechanism introduced in Java 5 and provides the different way of working with the input-output operations than the standard input-output API\u2019s.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-delete-file-example\/\">Java Nio Delete File Example<\/a><br \/>\nIf developers are working on a Java Swing or a desktop application then it may be required that sometimes developers need to delete a file from the file system. This tutorial is to learn about handling the files using the Java Nio package and shows how to delete a file in Java using the Nio package.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/file-nio\/java-nio-write-file-example\/\">Nio Write File Example<\/a><br \/>\nWith 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.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/file-nio\/path\/java-nio-file-path-example\/\">java.nio.file.Path Example<\/a><br \/>\nThis article introduces the Path interface and its basic usage. The Path interface is available in the Java SE 7 as part of Java NIO 2 File API. This article shows creating, getting information, converting and comparing paths. The examples in this article are compiled and run in Windows OS environment.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/file-nio\/watchevent\/java-nio-file-watchevent-example\/\">java.nio.file.WatchEvent Example<\/a><br \/>\nWatchEvent&lt;T&gt; is an interface defined in the java.nio.file package. The type parameter T is the type of the context object associated with the event. This interface has been in Java since Java SE 7 as part of NIO 2 File APIs. This is part of file change notification API, called the Watch Service API. Watch event in general represents an event or a repeated event for an object that is registered with a WatchService.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/io\/file\/4-ways-to-copy-file-in-java\/\">4 Ways to Copy File in Java<\/a><br \/>\nAlthough Java offers a class that can handle file operations, that is java.io.File, it doesn\u2019t have a copy method that will copy a file to another. The copying action is an important one, when your program has to handle many file related activities. Nevertheless, there are several ways you can perform a file copying operation in Java and we will discuss four of the most popular in this example.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filechannel\/create-memory-mapped-file\/\">Create memory mapped file<\/a><br \/>\nThis is an example of how to create a memory mapped file in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filelock\/create-shared-file-lock-on-file\/\">Create shared file lock on file<\/a><br \/>\nIn this example we shall show you how to create a shared file lock in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filelock\/create-file-lock-on-file\/\">Create file lock on file<\/a><br \/>\nThis is an example of how to create a file lock in Java.<\/li>\n<\/ul>\n<h4>Channels<\/h4>\n<ul>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-channels-example\/\">Java Nio Channels Example<\/a><br \/>\nChannels are the second major innovation of the Java Nio after buffers. In Java Nio, channels are used for the input-output transfers and this tutorial explains how the Java Nio Channels are used to open the network connections and connections to the files.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/filelock-channels\/java-nio-channels-filelock-example\/\">java.nio.channels.FileLock Example<\/a><br \/>\nThis 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.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-asynchronous-channels-tutorial\/\">Java Nio Asynchronous Channels Tutorial<\/a><br \/>\nThe Asynchronous Channels API\u2019s supplemented the core Java NIO API\u2019s with additional functionality in the Java 7 release. Coined NIO.2 the supplement provided many utilities for NIO usage but the crown jewel was the AsynchronousChannel API\u2019s.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-socketchannel-example\/\">Java Nio SocketChannel Example<\/a><br \/>\nSocketChannel is a selectable channel belonging to the java.nio.channels package and is used for reading or writing the stream-oriented data. In this tutorial, we learn how to use the SocketChannel and how it is used for reading or writing the stream-oriented data by using the TCP based protocol.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-filechannel-example\/\">Java Nio FileChannel Example<\/a><br \/>\nThe FileChannel is a Java Nio Channel implementation for working with a file. It facilitates reading, writing, mapping and manipulating a file. The examples in this tutorial will be demonstrated via test cases with no explicit Charset specified when encoding and decoding text from ByteBuffers.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filechannel\/copying-binary-file-with-filechannel\/\">Copying binary file with FileChannel<\/a><br \/>\nWith this example we demonstrate how to copy files using FileChannels in Java. In particular we are going to read data from a specific file in the file system and write them to another file.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filechannel\/create-stream-from-filechannel\/\">Create Stream from FileChannel<\/a><br \/>\nThis is an example of how to create input and output streams to read and write data from\/to a file in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/spi\/selectorprovider\/java-nio-channels-spi-selectorprovider-example\/\">java.nio.channels.spi.SelectorProvider Example<\/a><br \/>\n<code>SelectorProvider<\/code> is an abstract class defined in the <code>java.nio.channels.spi<\/code> package. This is a central service-provider class for selectors and selectable channels defined in the <code>java.nio.channels<\/code> 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.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/selector-channels\/java-nio-channels-selector-example\/\">java.nio.channels.Selector Example<\/a><br \/>\nThis 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.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/scatteringbytechannel\/java-nio-channels-scatteringbytechannel-example\/\">java.nio.channels.ScatteringByteChannel Example<\/a><br \/>\nScatteringByteChannel 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.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/asynchronouschannelgroup\/java-nio-channels-asynchronouschannelgroup-example\/\">java.nio.channels.AsynchronousChannelGroup Example<\/a><br \/>\nThis 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\u2019s 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.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/asynchronoussocketchannel\/java-nio-channels-asynchronoussocketchannel-example\/\">java.nio.channels.AsynchronousSocketChannel Example<\/a><br \/>\nThis article introduces the AsynchronousSocketChannel and its basic usage. This class is available since Java SE 7 as part of Java NIO 2 file API. This article\u2019s example shows the socket channel client sending messages to an AsynchronousServerSocketChannel server \u2013 in a client\/server setup. The example in this article is compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/completionhandler\/java-nio-channels-completionhandler-example\/\">java.nio.channels.CompletionHandler Example<\/a><br \/>\nThis article introduces the CompletionHandler and its basic usage. This interface is available since Java SE 7 as part of Java NIO 2 File API. This article\u2019s example shows reading from a file using asynchronous file channel and using the completion handler to consume its result. The example in this article is compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filechannel\/java-nio-channels-filechannel-example\/\">java.nio.channels.FileChannel Example<\/a><br \/>\nThis article introduces the FileChannel class and its basic usage. This class is available since Java 1.4 as part of Java NIO (New IO) File API. This article shows reading from and writing to file using file channels. The examples in this article are compiled and run in Windows OS environment. Note that Java SE 7 is required to run the code.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/channels\/asynchronousfilechannel\/java-nio-channels-asynchronousfilechannel-example\/\">java.nio.channels.AsynchronousFileChannel Example<\/a><br \/>\nThis article introduces the AsynchronousFileChannel class and its basic usage. This class is available since the Java SE 7 as part of Java NIO 2 File API. This article shows reading from and writing to file using asynchronous file channels.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-scattergather-example\/\">Java Nio Scatter\/Gather Example<\/a><br \/>\nIn Java Nio, the channel provides an important capability known as scatter\/gather or vectored I\/O in some circles. Scatter\/gather is a simple yet powerful concept and this tutorial explains how scatter\/gather can be really useful in situations where developers need to separate work with the various parts of the transmitted data.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-download-file-url-example\/\">Java Nio Download File From Url Example<\/a><br \/>\nJava NIO supports a buffer-oriented, channel-based approach for the I\/O operations and with the introduction of Java 7, the NIO system has expanded thereby providing the enhanced support for the file system features and the file handling mechanism. In this tutorial, we will see how to download a file from the URL in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-socket-example\/\">Java Nio Socket Example<\/a><br \/>\nThis article introduces the SocketChannel class and its basic usage. This class is defined in the java.nio package. 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.<\/li>\n<\/ul>\n<h4>Buffer<\/h4>\n<ul>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-bufferoverflowexception-example\/\">Java Nio BufferOverflowException Example<\/a><br \/>\nExceptions are the unwanted or the unexpected events that occur during the execution of programs that disrupt the normal flow of the instructions. In this tutorial, we will learn about the <code>BufferOverflowException<\/code> which is very common in the Java Nio package. But before moving ahead let\u2019s take a look and understand the basics of the Java Nio package.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-heapbytebuffer-example\/\">Java Nio HeapByteBuffer Example<\/a><br \/>\nThis example demonstrates the usage of the Java Nio HeapByteBuffer. The Java Nio HeapByteBuffer is an odd class, one you will never reference directly and for good reason, it\u2019s package private. Although it\u2019s use is almost guaranteed when working with ByteBuffers unless you opt for a DirectByteBuffer (off heap).<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/java-nio-bytebuffer-example\/\">Java Nio ByteBuffer Example<\/a><br \/>\nThis article is a tutorial on demonstrating the usage of the Java Nio ByteBuffer. All examples are done in the form of unit tests to easily prove the expectations of the API. The ByteBuffer class is an abstract class which also happens to extend Buffer and implement Comparable.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/buffer\/java-nio-buffer-example\/\">java.nio.Buffer Example<\/a><br \/>\nThis 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 also defined in the java.nio package.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/java-direct-bytebuffer-example\/\">Java Direct ByteBuffer Example<\/a><br \/>\nThis example shows the usage of direct ByteBuffer. ByteBuffer is an abstract class, extends Buffer and implements Comparable&lt;ByteBuffer&gt;. 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 of byte primitive type.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/mappedbytebuffer\/java-mappedbytebuffer-example\/\">Java MappedByteBuffer Example<\/a><br \/>\nIn this post, we are going to discuss about the class <code>java.nio.MappedByteBuffer<\/code>. There are two ways for reading a file, sequentially and randomly. Files that can be explored sequentially are known as sequential files. Files that permit random access to their contents are known as random access files (RAFs).<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/charbuffer\/convert-between-character-set-encodings-with-charbuffer\/\">Convert Between Character Set Encodings with CharBuffer<\/a><br \/>\nIn this example we shall show you how to convert between character set encodings with a CharBuffer in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filechannel\/write-to-channel-with-bytebuffer\/\">Write to Channel with ByteBuffer<\/a><br \/>\nWith this example we are going to demonstrate how to write data to a NIO Channel using a ByteBuffer in Java. In particular we are going to read data from a specific file in the file system and write them to a destination file.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filechannel\/read-from-channel-with-bytebuffer\/\">Read from Channel with ByteBuffer<\/a><br \/>\nThis is an example of how to read data from a NIO Channel using a ByteBuffer in Java. In particular we are going to read data from a specific file in the file system and print them on screen.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/filechannel\/save-changes-to-memory-mapped-bytebuffer-from-filechannel\/\">Save changes to memory mapped ByteBuffer from FileChannel<\/a><br \/>\nIn this example we shall show you how to write data to a file using a memory mapped ByteBuffer and a NIO FileChannel in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/write-append-to-file-with-byte-buffer\/\">Write\/Append to File with ByteBuffer<\/a><br \/>\nWith this is example we are going to demonstrate how to write\/append data to a file in Java using a ByteBuffer. Particularly we are going to read data from a source file and append them to the destination file.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/use-bytebuffer-for-non-byte-java-types-buffering\/\">Use ByteBuffer for non-byte Java types buffering<\/a><br \/>\nIn this example we will demonstrate how to perform non-byte Java types buffering using a ByteBuffer in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/use-bytebuffer-to-store-strings\/\">Use ByteBuffer to store Strings<\/a><br \/>\nThis is an example of how to store Strings using a ByteBuffer in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/get-byte-from-bytebuffer\/\">Get byte from ByteBuffer<\/a><br \/>\nWith this example we are going to demonstrate how to read bytes from a ByteBuffer. Additionally we will show you several of ByteBuffer\u2018s API methods in order to share some light on how to randomly ready data from it.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/put-byte-into-bytebuffer\/\">Put byte into ByteBuffer<\/a><br \/>\nThis is an example of how to put bytes into a ByteBuffer in Java. Additionally we will demonstrate several of ByteBuffer\u2018s API methods in order to share some light on how to randomly write data to it.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/create-direct-and-non-direct-bytebuffer\/\">Create direct and non-direct ByteBuffer<\/a><br \/>\nIn this example we are going to demonstrate several methods of creating a direct (memory-mapped) and non-direct ByteBuffer in Java.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/bytebuffer\/convert-between-bytebuffer-and-byte-array\/\">Convert between ByteBuffer and byte array<\/a><br \/>\nWith this example we are going to demonstrate how to convert between ByteBuffers and byte arrays.<\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/nio\/charbuffer\/convert-string-to-byte-array-utf-encoding\/\">Convert String to byte array UTF encoding<\/a><br \/>\nWith this example we are going to demonstrate how to convert a String to byte array and vice-versa using the default character encoding in Java. In short, to perform the aforementioned conversion, we are going to use classes from the NIO package in Java so as to convert every character from the target String to its byte equivalent.<\/li>\n<\/ul>\n<p>[undereg]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this detailed Resource page, we feature an abundance of Java NIO Tutorials! The java.nio (NIO stands for Non-blocking I\/O) is a collection of Java programming language APIs that offer features for intensive I\/O operations. It was introduced with the J2SE 1.4 release of Java by Sun Microsystems to complement an existing standard I\/O. NIO &hellip;<\/p>\n","protected":false},"author":34987,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-87426","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java NIO Tutorials - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn more about Java Non-blocking I\/O? Then check out our detailed Java NIO Tutorials and expand your knowledge!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/java-nio-tutorials\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java NIO Tutorials - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about Java Non-blocking I\/O? Then check out our detailed Java NIO Tutorials and expand your knowledge!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/java-nio-tutorials\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-06T14:04:03+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials\",\"name\":\"Java NIO Tutorials - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2019-02-08T11:05:48+00:00\",\"dateModified\":\"2023-03-06T14:04:03+00:00\",\"description\":\"Interested to learn more about Java Non-blocking I\\\/O? Then check out our detailed Java NIO Tutorials and expand your knowledge!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/java-nio-tutorials#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java NIO Tutorials\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java NIO Tutorials - Java Code Geeks","description":"Interested to learn more about Java Non-blocking I\/O? Then check out our detailed Java NIO Tutorials and expand your knowledge!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials","og_locale":"en_US","og_type":"article","og_title":"Java NIO Tutorials - Java Code Geeks","og_description":"Interested to learn more about Java Non-blocking I\/O? Then check out our detailed Java NIO Tutorials and expand your knowledge!","og_url":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_modified_time":"2023-03-06T14:04:03+00:00","og_image":[{"url":"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@javacodegeeks","twitter_misc":{"Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials","url":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials","name":"Java NIO Tutorials - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials#primaryimage"},"thumbnailUrl":"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2019-02-08T11:05:48+00:00","dateModified":"2023-03-06T14:04:03+00:00","description":"Interested to learn more about Java Non-blocking I\/O? Then check out our detailed Java NIO Tutorials and expand your knowledge!","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/java-nio-tutorials"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/java-nio-tutorials#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java NIO Tutorials"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/pages\/87426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/34987"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=87426"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/pages\/87426\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=87426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}