Send the link below via email or IMCopy
Present to your audienceStart remote presentation
- Invited audience members will follow you as you navigate and present
- People invited to a presentation do not need a Prezi account
- This link expires 10 minutes after you close the presentation
- A maximum of 30 users can follow your presentation
- Learn more about this feature in the manual
Do you really want to delete this prezi?
Neither you, nor the coeditors you shared it with will be able to recover it again.
Make your likes visible on Facebook?
Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.
Java 7,8 NIO.2 API features, a walkthrough
Transcript of Java 7,8 NIO.2 API features, a walkthrough
The Power of Java 7,8
Get educated with the new file I/O mechanism introduced
in the JDK 7,8 release.
Who am I?
By: Mohamed Taman
JCP EC, Systems Architect & Design supervisor
Java is the my best choices since 2001, I feel coding it like
Coding the Architecture
1- Working with the
2- Changing File Attributes with
3- What is
4- New API for
6- Traversal process using
7- Monitoring via
5- New powerful
But this part is all about how NIO.2
Added new Framework APIs, for powerful operations.....
This part is all about how NIO.2
streamlines & extends the I/O development power.....
We will talk about 8 topics
A path resides in a file system, which “
stores and organizes files on some form of media, generally one or more hard drives, in such a way that they can be easily retrieved.
This class is a
of NIO.2, and every application that involves I/O operations will exploit the powerful facilities of this class.
The file system can be accessed through the
final class, which is used to get an instance of the
we want to work on.
supports two types of operations
Almost any operation that involves manipulating paths without accessing the file system; these are logical manipulations done in memory.
Operations over files
referenced by paths.
Remember the days when you had to do the following?
File file = new File("index.html");
Do you remember this
Well, those days are gone, because with Java 7 you can do this:
Path path = Paths.get("index.html");
Moreover, starting with Java 7, both classes are available, which means programmers can mix their powers to obtain the best of I/O APIs.
Java 7 provides a simple API for conversion between them.
If you have ?
about a file or a directory,
Whether it is hidden?
Whether it is a directory?
What its size is, and who owns it?
You can get answers to those questions (and many others) from the
which is data about other data.
associates the notion of
with attributes and provides access to them through the
Since different file systems have different notions about which attributes should be tracked,
groups the attributes into
each of which maps to a particular file system implementation.
On the view, other methods are available
for additional tasks.
Linux and UNIX users (
) should be familiar with the concept of links.
There are two
types of links
Windows Users Know this as
Following are the main differences/similarities between the two types of links:
Hard links can be created only for files, not for directories. Symbolic links can link to a file or a directory.
Hard links cannot exist across file systems. Symbolic links can exist across file systems.
The target of a hard link must exist. The target of a symbolic link may not exist.
Removing the original file that your hard link points to does not remove the hard link itself, and the hard link still provides the content of the underlying file.
Removing the original file that your symbolic link points to does not remove the attached symbolic link, but without the original file, the symbolic link is useless.
If you remove the hard link or the symbolic link itself, the original file stays intact.
A hard link is the same entity as the original file. All attributes are identical. A symbolic link is not so restrictive.
A hard link looks, and behaves, like a regular file, so hard links can be hard to find.
A symbolic link’s target may not even exist, therefore it is much flexible.
commonly reach a file through several names, instead of navigating through a series of directories and subdirectories from the root.
Each method of the
class knows how to detect a
and will behave in the default manner if no configuration of behavior is specified.
ow that you know how to point to a file or directory using the
class in first point, you are ready to know how to accomplish the most common tasks for managing files and directories, such as
, and so on.
comes with a set of brand new methods to accomplish these tasks, most of which are found in the
provides some methods dedicated to checking if a Path is
...These checks enable you to determine what kind of file or directory you are dealing with before you apply operations such as write or read.
provides file operations,
Also you can work with the well-known
You will find at work methods for
streams, leaving coverage of the methods for channels for the next points 7, 8 and 9, in which you will see the real power of NIO.2.
Directory operations, allowing you to
directories. You will find how to list the file system roots, create directories with methods such as
, write directory filters, and list a directory’s content using the
represents an input source or an output destination (it can be anything from disk files to memory arrays).
Streams support different kinds of data, as strings, bytes, primitive data types, localized characters, and objects.
stream, each read or write request is handled directly by the underlying operating system, while in a buffered stream, the data is read from a memory area known as a buffer; and the native input API is called only when the buffer is empty.
output streams write data to a buffer, and the native output API is called only when the buffer is full. When a buffer is written out without waiting for it to fill, we say that the buffer is flushed.
nterface for Uni
User defined attributes.
) access to a file’s contents Is it permits random access to their contents, and are known as random access files (
With a RAF, you can open the file, seek a particular location, and read from or write to that file.
After you open a
you can read from or write to it in a random manner just by using a record number, or you can add to the beginning or end of the file since you know how many records are in the file.
Is RAF powerful....
Files that can be explored sequentially are known as
files are used more often because they are easy to create, but
are more flexible and their data can be located faster.
allows you to read a single character, read a chunk of bytes or a line, replace a portion of the file, append lines, delete lines, and so forth, and allows you to perform all of these actions in a random manner.
Java 7 (
) introduces a brand-new interface for working with
Its name is
and it is available in the
It extends the older
interface and represents a byte channel that maintains a current position and allows that position to be modified.
improves the well-known
class by implementing this interface and providing RAF and
power in a single shot. With a simple cast we can transform a
provides facilities such as mapping a region of the file directly into memory for faster access, locking a region of the file, and reading and writing bytes from an absolute location without affecting the channel’s current position.
interface is based on the old interfaces
(represents a channel that reads bytes; only one thread can read at a time) and
(represents a channel that writes bytes; only one thread can write at a time) that have been available in
. These two interfaces are super interfaces for
Thanks to this relationship between them.
Recursive Operations: Walks
takes advantage of recursion programming technique.
Based on this mechanism,
encapsulates the traversal process of a file tree in an interface, named
, in the
This is a very common approach when performing tasks such as
a file tree.
As previously mentioned, the
interface provides the support for recursively traversing a file tree.
The methods of this interface represent key points in the traversal process, enabling you to take control when a file is visited, before a directory is accessed, after a directory is accessed, and when a failure occurs.
, this interface has hooks for before, during, and after a file is visited, as well as for when failure occurs.
Once you have control (at any of these key points), you can choose how to process the visited file and decide what should happen to it next by indicating a visit result through the
File notifications monitoring
Watch service API
The Watch Service API was introduced in Java 7 (
) as a thread-safe service that is capable of watching objects for changes and events.
The most common use is to monitor a directory for changes to its content through actions such as
You’ve probably seen the effect of such a service many times.
The Watch Service API is a low-level API that can be used as is or can be customized.
By default, this API uses the underlying file system functionalities to watch the file system for changes.
When one or more of the
events are detected by the watch service, the watch service passes the notification events to the process that is registered to handles them through a separate thread or pool of threads.
You no longer need to poll the file system for changes or use other
solutions to monitor the file system changes.
You can even write a high-level API on top of it.
Java introduced support for sockets in
, but things have of course changed over time from version to version.
Jumping to Java 7,
has improved this support by updating existing classes with new methods and adding new interfaces/classes for writing
First of all,
introduces an interface named
that provides methods commons to all network channel classes — any channel that implements this interface is a channel to a network socket.
is like a telephone,
is like a letter. The sender writes the receiver address (remote IP and port) and sender address (local IP and port) on the envelope
packet), puts the letter (data to be sent) into the envelope, and sends the letter.
He doesn’t know if the letter will arrive to the receiver or not. Moreover, a more recent letter can arrive faster than and old one, and a letter might never arrive at all—the letters are not aware of one another.
is for high-reliability data transmissions while
is for low-overhead transmissions. Typically, use
in applications in which reliability is not critical but speed is.
The main classes dedicated to synchronous socket channels,
implement this interface, which comes with methods for binding to and returning local addresses, and methods for setting and getting socket options through the new
This interface’s methods and the ones added directly into classes (for checking connection state, getting remote addresses, and shutdown) will prevent you from having to call the
also introduces the
interface as a sub interface of
As its name suggests, the
interface maps a network channel that supports IP multicasting.
Keep in mind that
is implemented only by the datagram channel (the
When joining a multicast group you get a membership key, which is a token that represents the membership of a multicast group.
Many real-life cases can be programmed based on multicasting, such as
online conferencing, news distribution, advertising, e-mail groups,
When you decide to write a Java TCP server/client application, you must consider whether you need to write a blocking or non-blocking application. This decision is important because the implementations are different and the complexity may also be critical.
The main characteristic of a blocking mechanism presumes that a given thread cannot do anything more until the I/O is fully received, which may take a while in some cases—the application’s flow is blocked because the methods do not return right away.
Non-blocking mechanisms, on the other hand, immediately queue an I/O request and return the control to application flow (methods return right away). The request will be processed later by the kernel.
From a Java developer perspective, you also must take into account the degree of complexity involved by these mechanisms. Non-blocking mechanisms are much more complex to implement than blocking mechanisms, but they allow you more performance and scalability.
Keep in mind...
that even non-blocking looks like a multithreading application, this is a single-thread application based on the multiplexing technique.
In other words
, with a non-blocking mechanism, a function won’t wait while on the stack, and with an asynchronous mechanism, work may continue on behalf of the function call after that call has left the stack.
The Jewel of
We’ve finally reached the most powerful feature introduced in
asynchronous channel API
the asynchronous I/O
Java 7 journey starts in the
interface, which extends a channel with asynchronous I/O operations support.
This interface is implemented by three classes:
This is a huge mountain
In addition, there is an asynchronous channel named
that can read and write bytes and stands up as a sub interface of
(this sub interface is implemented by the
Moreover, the new API introduces a class named
which presents the concept of an asynchronous channel group, in which each asynchronous channel belongs to a channel group (
the default one or a specified one
) that shares a pool of Java threads.
These threads receive instructions to perform I/O events and they dispatch the results to the completion handlers. All the effort is for the purpose of handling the completion of initiated asynchronous I/O operations.
Thread Pool !!!
A thread enters into action and waits until the I/O request is completed (the program is
waiting for the process to end, with no way out).
When the same action occurs in an asynchronous environment, a thread performs the I/O operation with more kernel help.
Actually, it immediately passes the request to the kernel and continues on to process another job.
The kernel signals to the thread when the operation has completed, and the thread
the signal by interrupting its current job and processing the data from the I/O operation as necessary.
The difference between synchronous and asynchronous execution may seem a bit confusing at first, so let’s clear it up.
In the Java spirit of platform independence, asynchronous I/O can be tied to multiple threads—basically, allowing something to be processed on a separate thread.
More and More.....
I/O serve different purposes. You can use synchronous I/O if you simply want to make a request and receive a response.
Asynchronous I/O is a different programming model, because you don’t necessarily wait for a response, but rather submit your work for execution and then come back for a response either almost immediately or sometime later.
I/O in Java, we are talking about the asynchronous channels.
An asynchronous channel is a connection that supports multiple I/O operations in parallel through separate threads (connecting, reading, and writing, for example)
and provides mechanisms for controlling the operations after they’ve been initiated.
All asynchronous I/O
operations have one of two forms
I/O processing that is expected to take a large amount of time can be optimized by using
I/O. For relatively fast I/O operations,
I/O would be better because the overhead of processing kernel I/O requests and kernel signals may make asynchronous I/O less beneficial.
I/O limits performance and scalability since it is one thread per I/O connection, and running thousands of threads significantly increases overhead on the operating system.
I/O seems to be better than synchronous I/O, since performance and scalability are keywords of the I/O system. Various important operating systems, such as Windows and Linux, support fast, scalable I/O based on the use of asynchronous notifications of I/O operations taking place in the OS layers.
That all asynchronous channels initiate I/O operations (does not block the application to perform other tasks) and provide notifications when I/O completes.
Attending & Listening.
Think of a
as an entity mapping a file/directory path and identified through a set of names.
Asynchronous is more familiar with parallel (as threading), while non-blocking often refers to polling.
Basically, there are two types of input/output (I/O) synchronization: synchronous I/O and asynchronous I/O (also referred to as overlapped I/O).
is good for sending messages from one system to another when the order isn’t important and you don’t need all of the messages to get to the other machine.
This rule is the foundation of asynchronous channels, and from it derives the entire asynchronous channel API.
Creating a Path.
Retrieving Information About a Path.
Removing Redundancies from a Path.
Converting a Path.
Joining Two Paths.
Creating a Path Between Two Paths.
Comparing Two Paths.
Creating a path is simple as:
Path p1 = Paths.get("/tmp/foo");
Path p2 = Paths.get(args);
Path p3 = Paths.get(URI.create("file:///Users/taman/Test.java"));
is a shorthand for:
Path p4 = FileSystems.getDefault().getPath("/users/taman");
You have a
instance representing a file or directory, but does that file exist on the file system? Is it readable? Writable? Executable?
To convert the following path:
Path path = get("C:", "JavaOne2012/HOL2846-NIO2", "src/resources", "HOL2846_TOC.txt");
An Absolute path:
A Real Path:
for text files.
use with existing APIs
Channels and byte
file-locking, memory-mapped IO
File I/O Methods Arranged from Less Complex to More Complex
Many methods didn't throw exceptions when they failed.
The rename method didn't work consistently across platforms.
There was no real support for symbolic links.
More support for metadata was desired, such as file permissions, file owner, and other security attributes.
Accessing file metadata was inefficient.
Many of the File methods didn't scale. ex. Requesting a large directory listing over a server could result in a hang. Large directories could also cause memory resource problems, resulting in a denial of service.
It was not possible to write reliable code that could recursively walk a file tree and respond appropriately if there were circular symbolic links.
Prior to the Java
class was the mechanism used for file I/O, but it had several drawbacks:
Systems Architect & Design supervisor
for Egyptian ministry of finance company “
JCP & EC
Member, responsible for JSRs revision, evaluation, RI testing and evangelist the new technologies specifications & JCP process standardization.
Beside that I am one of the board member of “
Oracle Egypt Architect Club
A frequent Speaker
JavaOne, Devoxx, Oracle Days,Tunis JUG Day, jMaghreb, JEEConf & JDC... etc
Consultant, Trainer, Blogger, & articles writer, books author and tech. reviewer.
Regularly posts on my blog , and also on