Old NIO has many drawbacks, for e.g., some methos just do not throw nay exceptions when failing;
file deletion will not return any errors like "file not found, permission not allowed"
Rename worked inconsistently
Four key new helper Types new in Java 7
Class java.nio.file.Paths which is used to get a Path
Exclusively static methods to return a Path by converting a string or Uniform Resource Identifier(URI)
Interface java.nio.file.Path
Used for objects that represent the location of a file in a file system
Class java.no.file.Files which is used to do stuff
Exclusively static methods to operate on files , directories and other types of files
Class java.nio.file.FileSystem
Example of Helpers in Action
FIle copy is really easy with grain control and File move is supported as well
Path src=Paths.get("/home/a.txt");
Path dst=Pahts.get("/home/infor/b.txt")
Files.copy(src, dst, StandCopyOption.COPY_ATTRIBUTES)
NIO.2 Directories
DirectoryStream iterate over entries
scales to large directories
users less resources
smooth out response time for remote file systems
Implement Iterable and Closeable for productivity
Filtering support
built-in support for glob, regex and custom filters
srcPath=Paths.get("/home/oracle");
try(DirecotryStream<Path> dir = srcPath.newDirectoryStream("*.java"))
{
for (Path file: dir)
System.out.println(file.getName();
}
Custom filesystem
watching a directory
Walking a fiel tree
More information on NIO2
OpenJDK:NIO project website on java.net
Includes resourcefs for
multicasting
Asynchronous I/O
Creating your own file system implementation
File I/O Tutorial
Java Spotlight Podcase
What Is a Path? (And Other File System Facts)
A file system 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. Most file systems in use today store the files in a tree (or hierarchical) structure. At the top of the tree is one (or more) root nodes. Under the root node, there are files and directories (folders in Microsoft Windows). Each directory can contain files and subdirectories, which in turn can contain files and subdirectories, and so on, potentially to an almost limitless depth.
This section covers the following:
What Is a Path?
The following figure shows a sample directory tree containing a single root node. Microsoft Windows supports multiple root nodes. Each root node maps to a volume, such as
C:\
orD:\
. The Solaris OS supports a single root node, which is denoted by the slash character,/
.![]()
Sample Directory Structure A file is identified by its path through the file system, beginning from the root node. For example, the
statusReport
file in the previous figure is described by the following notation in the Solaris OS:/home/sally/statusReportIn Microsoft Windows,
statusReport
is described by the following notation:C:\home\sally\statusReportThe character used to separate the directory names (also called the delimiter) is specific to the file system: The Solaris OS uses the forward slash (
/
), and Microsoft Windows uses the backslash slash (\
).Relative or Absolute?
A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file. For example,
/home/sally/statusReport
is an absolute path. All of the information needed to locate the file is contained in the path string.A relative path needs to be combined with another path in order to access a file. For example,
joe/foo
is a relative path. Without more information, a program cannot reliably locate thejoe/foo
directory in the file system.Symbolic Links
File system objects are most typically directories or files. Everyone is familiar with these objects. But some file systems also support the notion of symbolic links. A symbolic link is also referred to as a symlink or a soft link.
A symbolic link is a special file that serves as a reference to another file. For the most part, symbolic links are transparent to applications, and operations on symbolic links are automatically redirected to the target of the link. (The file or directory being pointed to is called the target of the link.) Exceptions are when a symbolic link is deleted, or renamed in which case the link itself is deleted, or renamed and not the target of the link.
In the following figure,
logFile
appears to be a regular file to the user, but it is actually a symbolic link todir/logs/HomeLogFile
.HomeLogFile
is the target of the link.![]()
Example of a Symbolic Link. A symbolic link is usually transparent to the user. Reading or writing to a symbolic link is the same as reading or writing to any other file or directory.
The phrase resolving a link means to substitute the actual location in the file system for the symbolic link. In the example, resolving
logFile
yieldsdir/logs/HomeLogFile
.In real-world scenarios, most file systems make liberal use of symbolic links. Occasionally, a carelessly created symbolic link can cause a circular reference. A circular reference occurs when the target of a link points back to the original link. The circular reference might be indirect: directory
a
points to directoryb
, which points to directoryc
, which contains a subdirectory pointing back to directorya
. Circular references can cause havoc when a program is recursively walking a directory structure. However, this scenario has been accounted for and will not cause your program to loop infinitely.The next page discusses the heart of file I/O support in the Java programming language, the
Path
class.