The Best Technical Blog Ever

twitterrss

The Power of New Java File I/O (NIO.2)

It refreshes the NIO library introduced in 2002 with Java 1.4. The NIO.2 has come forward with many enhancements. New classes are introduced to ease the life of a developer when working with multiple file systems. Java 7 introduced java.nio.file package and its related package, java.nio.file.attribute, to provide full support for file I/O and for accessing the default file system. It also introduced the Path class which allow you to represent any path in operating system.


New File system API complements older one and provides several useful methods for checking, deleting, copying, and moving files. In this post, you will learn how to work with file systems-for example, how to save a file/directory; create a file/directory; navigate directories; copy, move, or delete a file/directory; and so on. As a Java programmer, you should be aware of these file/directory operations. Before you know how to carry out these easy file operations, you have to learn about the Path interface that replaces the File class. Next, you will see how to read and write files and finally, we will see file and directory operations with examples. This section will cover the following:


  • Use the Path class to operate on file and directory paths
  • Use the Files class to perform file operations on a path
  • Commonly Used File Attributes
  • Creating files and directories in NIO.2
  • Create temporary files and directories using Java NIO2
  • Read and change file/directory attributes
  • Watching a Directory for Modifications


1. Use the Path class to operate on file and directory paths:

The Path class is a programmatic representation of a path in the file system. The first component of a path may be a root component, such as / or C:\. A Path object contains the file name and directory list used to construct the path. It is used to examine, locate, and manipulate files. A path that starts with a root component is absolute path. Otherwise, it is relative path.


In Java 6 and earlier you do that:


Examples:
File file = new File("SOL.TXT");

In Java 7 you do that:


Examples:
// Paths class consists exclusively of static methods that 
//return a Path by converting a path string or URI

Path path = Paths.get("SOL.TXT");
Creating a Path:

A Path is simply a reference to a file path. You can create a Path object by using get method. The Paths.get method is shorthand for the following code:


Examples:
Path p1 = FileSystems.getDefault().getPath("java\\a.txt");

It receives one or more strings, which it joins with the path separator of the default file system (/ for Solaris , \\ for Windows).


Example :
path


You can also provide a string with separators to the Paths.get method:


Example :
path


Getting Path Information

Let's create a Path object and retrieve the basic information associated with the object.


Example :
path

Output :
path


Comparing Paths:

The equals() method is used to compare two Path objects for equality based on their textual(string) representation.


Example :
path

You can also use the startsWith() and endsWith() methods to test if a path starts with and ends with a given path, respectively.


Example :
path

Example :
path

Joining Two Paths:

Using the resolve method, you can combine paths . You can pass a partial path (without root element) in resolve method for appending it with the original path.


Example :
path

To yield sibling path, resolveSibling() is used against a path's parent.


Example :
path

Creating a Path Between Two Paths:

To construct a path from one location in the file system to another location use the relativize method. This method constructs a path originating from the original path and ending at the location specified by the passed-in path. The new path is relative to the original path.


Example :
path

Removing Redundancies From a Path:

The normalize() method returns a path by eliminating the redundant name elements. The retrieved path does not contain redundant name elements. In many file systems, the "." and ".." are special names used to indicate the current directory and the parent directory. For example, all occurrences of "." are considered as redundant.


Example :
path

In this example, the two paths share the same node, home. To navigate from home to java, you first navigate one level down to tutorial and then one more level down to java. Navigating from java to home requires moving up two levels.


Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status