CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > java.nio Package in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor

java.nio Package in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

The java.nio package provides a set of APIs for efficient non-blocking input/output (I/O) operations. Java NIO, short for New I/O, was introduced in Java 1.4 as an alternative to the traditional I/O package (java.io). The NIO API is based on the buffer-oriented approach, which means that data is read and written to and from buffers. Buffers are efficient containers for data that can be easily manipulated and transferred.

The NIO package is divided into the following subpackages:

  • java.nio.channels: This subpackage defines classes that represent channels, which are used to perform I/O operations on underlying resources such as files, sockets, and network interfaces.
  • java.nio.charset: This subpackage defines classes for encoding and decoding data between different character sets.
  • java.nio.file: This subpackage defines classes for accessing and managing files and file systems.

The java.nio package is used in a wide variety of applications, including:

  • Web servers
  • Network applications
  • File transfer applications
  • Database applications
  • Real-time systems

Benefits of using the java.nio package

There are several benefits to using the java.nio package:

  • Performance: The NIO API is designed for high performance. It uses efficient buffering and non-blocking I/O operations to minimize overhead.
  • Scalability: The NIO API is scalable to handle large numbers of concurrent I/O operations.
  • Flexibility: The NIO API is flexible and can be used to implement a wide variety of I/O scenarios.

Example of using the java.nio.channels package

The following example shows how to use the java.nio.channels package to read data from a file:

import java.nio.channels.FileChannel;
import java.io.FileInputStream;

public class Main {
        public static void main(String[] args) throws Exception {
		// Create a FileChannel for the file to be read. The file is located in Desktop location. 
		FileChannel channel = new FileInputStream(
				"C:\\Users\\codersathi\\Desktop\\hello.txt").getChannel();

		// Create a ByteBuffer to store the data read from the file.
		ByteBuffer buffer = ByteBuffer.allocate(1024);// You can adjust the buffer size as
														// needed

		while ((channel.read(buffer)) != -1) {
			buffer.flip(); // Flip the buffer to read data

			while (buffer.hasRemaining()) {
				System.out.print((char) buffer.get()); // Print the characters
			}
			/*
			 * Clear the buffer for the next read. If we don't clear it, then the loop
			 * will continue forever. You can check it first with buffer.clear() and later
			 * without buffer.clear().
			 */
			 buffer.clear();
		}

		// Close the channel.
		channel.close();
	}
}

This example will read the contents of the file hello.txt and print them to the console.

Related Posts:

  • Control Statements in Java
  • What is Try-With-Resources in Java? A Complete Guide
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • What is Java? Exploring Its Key Features, Benefits,…
  • Packages in Java: A Guide to Modular, Maintainable Code
  • A Brief History Of Java
Tags:iojava
Was this article helpful?
← Previous ArticleUnderstanding String Interning in Java
Next Article →Define Multipart File Upload Size Limit in Spring Boot Application

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap